&++x Lvalue but &x++ not?

Started by
16 comments, last by TheComet 10 years, 3 months ago

In the 6.2.1 Results section page on 84 chapter for of the C++ programming language by Bjarne Stroustrup:

int* p = &++x; // p points to x
int* q = &(x++); // error: x++ is not an lvalue (it is not the value stored in x)

Why is the second statement's x++ not an lvalue?

According to the precedence chart, both postfix and prefix unary increment operators are executed before the address operator, and until the sequence point is reached(semicolon in both cases) the side-effect is executed(x+=1.)

In other words, if the first statement's x is considered an lvalue, so should the second statment's x.

It would seem that x++ leaves the rvalue(whatever literal value x refers to, AKA referent) while ++x leaves the lvalue(referer) as the immediate evaluation.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
Advertisement
If I were on my PC I would explain it in more detail, and typing on this device is slow so I'll allow others to cite the why's and wherefores.

The simple rule for all code is no more than one modification per statement. More than one causes confusion at best, undefined behavior at worst. It is an extremely common source of bugs to have multiple modifications per statement (even indirectly through function calls that modify things). Best practice is to avoid it entirely.

If you must increment, do that in a separate statement.
The way I think about it, ++x can be implemented as "increment and return the current value". Taking the address of the result gives us the address of x, which is fine. Now consider that x++ is something like "increment and return the old value". The old value must necessarily be a temporary, so taking its address is a recipe for disaster, because by the time you want to do something with the pointer you just obtained, the temporary will be gone.

There is probably a more formal description of the situation, but perhaps my explanation is easier to understand.

int* p = &++x; // p points to x
int* q = &(x++); // error: x++ is not an lvalue (it is not the value stored in x)

in first case pre-increment returns a lvalue thats persist in memory, where as in second case post-increment being a rvalue is a temp.

The post-increment is equal to the following code segment:


int postincrement(int& x)
{
   int oldX = x;
   x+=1;
   return oldX;
}

Hope this makes it more clear why x++ does not return an lvalue.

devstropo.blogspot.com - Random stuff about my gamedev hobby

Thats why preincrement is usually tiny bit faster than postincrement when used in for-loop. Because it doesn't need intermediate temporary variable to store the result of the operation.

Thats why preincrement is usually tiny bit faster than postincrement when used in for-loop. Because it doesn't need intermediate temporary variable to store the result of the operation.


Only for types with a non-trivial preincrement/postincrement operator implementation, such as some iterators; for things like ints, it's in practice exactly the same, because the compiler sees that nothing uses the intermediate temporary and elides it.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

[...] consider that x++ is something like "increment and return the old value". The old value must necessarily be a temporary, so taking its address is a recipe for disaster, because by the time you want to do something with the pointer you just obtained, the temporary will be gone.

[...]

Ah, good mnemonic. I wonder if the register is incapable of loading both values(next and previous, AKA old and incremented) and therefore your idea of temporary rvalue(that will be deallocated after the next sequence point) actually happens.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

The post-increment is equal to the following code segment:


int postincrement(int& x)
{
   int oldX = x;
   x+=1;
   return oldX;
}

Hope this makes it more clear why x++ does not return an lvalue.

Yes, because it is an automatic variable whose scope and duration will expire(after the function returns.)

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

it's in practice exactly the same, because the compiler sees that nothing uses the intermediate temporary and elides it.

Exactly. Have you got any assembly code to be examined to understand it a bit better?

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

This topic is closed to new replies.

Advertisement