简体   繁体   中英

Why is there a prefix/postfix ++ but no prefix/postfix +=?

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.

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.

只要y没有副作用:

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

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.

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.

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

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM