Advertisement Jump to content
Sign in to follow this  
gasto

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!