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

This topic is 2087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites

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.

##### Share on other sites
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.

##### Share on other sites

[...] 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.

##### Share on other sites

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.)

##### Share on other sites

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?

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 18
• 12
• 9
• 9
• 25