• Advertisement
Sign in to follow this  

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

This topic is 1454 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

Just like x = x+5 won't work but x += 5 will work in a for loop

Edited by rip-off
When i tried it weeks ago, it didn't work but when i tried it after i posted that, it worked.

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.


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.

 

 

I haven't said that it is somehow significantly faster, just pointed out the way it actually works.

Share this post


Link to post
Share on other sites

Just like x = x+5 won't work but x += 5 will work in a for loop 

Can you explain what you mean by won't work?
When i tried it weeks ago, it didn't work but when i tried it after i posted that, it worked.
That's why it's edited out.

Share this post


Link to post
Share on other sites

That's ok. We'd prefer if you didn't edit earlier posts, as it can make the thread more difficult to follow for others. A better approach would be to edit the post and strike through any errors in the original, and add a explanation.

 

Here is an example where I corrected a mistake I made in this way. I'm going to restore your earlier post like that.

Share this post


Link to post
Share on other sites

That's ok. We'd prefer if you didn't edit earlier posts, as it can make the thread more difficult to follow for others. A better approach would be to edit the post and strike through any errors in the original, and add a explanation.

 

Here is an example where I corrected a mistake I made in this way. I'm going to restore your earlier post like that.

I think it would be beneficial if he stated what compiler he was using. I've used MinGW and GCC for years and both methods have always worked for me for as long as I can remember. This way we know if it is just a fluke that happened or if a compiler, for whatever reason, is playing (for lack of a better word) willy-nilly with the standard and how the code works.

Edited by BHXSpecter

Share this post


Link to post
Share on other sites

What people are referring to as "a temporary value" is known in C/C++ as an rvalue. The postfix unary increment returns an rvalue, whereas the prefix unary increment returns an lvalue.

 

This can be seen if you run the following code, compiled with C++11:

#include <iostream>

void print( const int& value )
{
    std::cout << "this is an lvalue" << std::endl;
}
void print( const int&& value )
{
    std::cout << "this is an rvalue" << std::endl;
}

int main()
{
    int x = 0;
    print( ++x );
    print( x++ );
    return 0;
}

A more detailed explanation on lvalues and rvalues can be obtained here:

http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx

 

Another approach in explaining this behaviour can be done by examining unary increment overloads in a custom class:

class X {
    int m_Value;
public:
    X& operator++()
    {
        ++m_Value;
        return *this;
    }
    X operator++(int)
    {
        X tmp(*this);
        operator++();
        return tmp;
    }
};

Your example using the class:

X x;
X* p1 = &++x; // this calls operator++(), which returns a reference to itself (return *this;), i.e. getting the address is valid.
X* p2 = &(x++); // this calls operator++(int), which returns the temporary after incrementing, i.e. getting the address is invalid.
Edited by TheComet

Share this post


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

  • Advertisement