为什么有前缀/后缀 ++但没有前缀/后缀 +=?

发布于 2024-10-11 12:03:34 字数 438 浏览 6 评论 0原文

这似乎是一个愚蠢的问题,但为什么在许多语言中都存在 ++-- 运算符的前缀和后缀版本,但没有类似的前缀/postfix 其他运算符的版本,例如 +=-=?例如,看起来如果我可以编写这段代码:

myArray[x++] = 137; // Write 137 to array index at x, then increment x

我应该能够编写类似的内容

myArray[5 =+ x] = 137; // Write 137 to array index at x, then add five to x

当然,这样的运算符不存在。这是有原因的吗?这在 C/C++/Java 中似乎是一种奇怪的不对称性。

This may seem like a silly question, but why is it that in many languages there exists a prefix and postfix version of the ++ and -- operator, but no similar prefix/postfix versions of other operators like += or -=? For example, it seems like if I can write this code:

myArray[x++] = 137; // Write 137 to array index at x, then increment x

I should be able to write something like

myArray[5 =+ x] = 137; // Write 137 to array index at x, then add five to x

Of course, such an operator does not exist. Is there a reason for this? It seems like a weird asymmetry in C/C++/Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雨后彩虹 2024-10-18 12:03:34

我猜想有几个原因,我认为其中权重较大的可能是:

  • 的一些语言设计者甚至可能没有想到)
  • 可能没有被认为有太多的实际用例(早期 后增量直接映射到机器操作(至少在几台机器上),因此它们找到了进入该语言的方式(更新:事实证明这并不完全正确,即使在计算知识中通常如此认为。见下文) 。

话又说回来,虽然前/后/递增/递减运算符的想法可能受到机器操作的影响,但看起来它们并没有专门放入语言中以利用这种优势。以下是 Dennis Ritchie 对它们的评价:

http://cm.bell-labs。 com/cm/cs/who/dmr/chist.html

汤普森更进一步,发明了 ++ 和 -- 运算符,它们可以递增或递减;它们的前缀或后缀位置决定了更改是发生在记录操作数的值之前还是之后。它们并不出现在 B 的最早版本中,而是一路出现的。人们经常猜测它们是为了使用 DEC PDP-11 提供的自动递增和自动递减地址模式而创建的,C 和 Unix 正是在该模式上首次流行的。这在历史上是不可能的,因为开发 B 时还没有 PDP-11。然而,PDP-7 确实有一些“自动递增”内存单元,其特性是通过它们进行间接内存引用会递增单元。这个功能可能向 Thompson 推荐了这样的运算符;使它们成为前缀和后缀的概括是他自己的。事实上,自增单元格并没有直接用于运算符的实现,而更强烈的创新动机可能是他观察到++x的平移小于x=x+1的平移。

I'd guess there are several reasons, I think among the more heavily weighted might be:

  • there probably weren't thought to be too many real use cases (it may not have even occurred to some language designers in the early days)
  • pre/post increment mapped directly to machine operations (at least on several machines), so they found their way into the language (update: it turns out that this isn't exactly true, even if it's commonly thought so in computing lore. See below).

Then again, while the idea for pre/post/increment/decrement operators might have been influenced by machine operations, it looks like they weren't put into the language specifically to take advantage of such. Here's what Dennis Ritchie has to say about them:

http://cm.bell-labs.com/cm/cs/who/dmr/chist.html

Thompson went a step further by inventing the ++ and -- operators, which increment or decrement; their prefix or postfix position determines whether the alteration occurs before or after noting the value of the operand. They were not in the earliest versions of B, but appeared along the way. People often guess that they were created to use the auto-increment and auto-decrement address modes provided by the DEC PDP-11 on which C and Unix first became popular. This is historically impossible, since there was no PDP-11 when B was developed. The PDP-7, however, did have a few `auto-increment' memory cells, with the property that an indirect memory reference through them incremented the cell. This feature probably suggested such operators to Thompson; the generalization to make them both prefix and postfix was his own. Indeed, the auto-increment cells were not used directly in implementation of the operators, and a stronger motivation for the innovation was probably his observation that the translation of ++x was smaller than that of x=x+1.

眼波传意 2024-10-18 12:03:34

只要 y 没有副作用:

#define POSTADD(x,y) (((x)+=(y))-(y))

As long as y has no side effects:

#define POSTADD(x,y) (((x)+=(y))-(y))
静赏你的温柔 2024-10-18 12:03:34

我会做一个假设。 ++i/i++ 有很多用例,并且在许多用例中,特定类型的增量(前/后)会有所不同。我已经记不清有多少次看到像 while (buf[i++]) {...} 这样的代码。另一方面,+= 的使用频率要低得多,因为一次将指针移动 5 个元素几乎没有意义。

因此,没有足够常见的应用程序可以使 += 的后缀版本和前缀版本之间的差异变得很重要。

I'll make an assumption. There're lots of use-cases for ++i/i++ and in many the specific type of increment (pre/post) makes difference. I can't tell how many times I've seen code like while (buf[i++]) {...}. On the other hand, += is used much less frequently, as it rarely makes sense to shift pointer by 5 elements at once.

So, there's just no common enough application where difference between postfix and prefix version of += would be important.

豆芽 2024-10-18 12:03:34

我想这是因为它太神秘了。
有些人认为甚至应该避免使用 ++/--,因为它们会引起混乱并导致大多数缓冲区溢出错误。

I guess it's because it's way too cryptic.
Some argue that even ++/-- should be avoided, because they cause confusion and are responsible for most buffer overrun bugs.

往昔成烟 2024-10-18 12:03:34

因为 -- 和 ++ 运算符映射到 CPU 中的 inc(rement) 和 dec(rement) 指令(除了加法和减法之外),并且这些运算符是应该映射到指令,因此它们作为单独的运算符存在。

Because the -- and ++ operators map to inc(rement) and dec(rement) instructions (in addition to adding and subtracting) in the CPU, and these operators are supposed to map to the instructions, hence why they exist as separate operators.

别理我 2024-10-18 12:03:34

Java 和 C++ 具有前置和后置自增和自减运算符,因为 C 也有它们。 C 拥有它们是因为 C 主要是为 PDP-11 编写的,而 PDP-11 有 INCDEC 指令。

过去,优化编译器还不存在,因此如果您想使用单循环增量运算符,要么为它编写汇编程序,要么您的语言需要为其显式运算符; C 是一种可移植的汇编语言,具有显式的递增和递减运算符。此外,++ii++ 之间的性能差异现在已经不太重要,但在 1972 年确实很重要

。请记住,C 已有近 40 年的历史了。

Java and C++ have pre- and post- increment and decrement operators because C has them. C has them because C was written, mostly, for the PDP-11 and the PDP-11 had INC and DEC instructions.

Back in the day, optimizing compilers didn't exist so if you wanted to use a single cycle increment operator, either you wrote assembler for it or your language needed an explicit operator for it; C, being a portable assembling language, has explicit increment and decrement operators. Also, the performance difference between ++i and i++ rarely matters now but it did matter in 1972.

Keep in mind that C is almost 40 years old.

浮生未歇 2024-10-18 12:03:34

如果我不得不猜测,通常将:

x += 5;

...与:

x = x + 5;

出于显而易见的原因,这是荒谬的写:

x + 5 = x;

我不知道你还能如何仅使用 +5 =+ x 的行为> 运算符。顺便说一句,你好!

If I had to guess, it's common to equate:

x += 5;

...with:

x = x + 5;

And for obvious reasons, it would be nonsensical to write:

x + 5 = x;

I'm not sure how else you would mimic the behavior of 5 =+ x using just the + operator. By the way, hi htiek!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文