• Advertisement

Archived

This topic is now archived and is closed to further replies.

rvalues and lvalues of class type

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

Are there any differences between rvalues of class type, and lvalues of class type? They seem to behave exactly the same.

Share this post


Link to post
Share on other sites
Advertisement
An "lvalue" can appear on the left hand side of an assignment statement.

This means, an lvalue can be something like a reference, variable, dereferenced pointer, array element etc.

A value which is not a lvalue cannot appear on the left of assignment, which would be something like:
- A constant value
- A const reference
- A dereferenced const pointer
- A computed value where the result is not itself an lvalue

It does not make sense to do


int x = 42;
(x+1) = 99;


because (x+1) is not an lvalue.

Mark

Share this post


Link to post
Share on other sites
But an rvalue of class type can appear on the left hand side of an assignment expression.


class A{};

A func()
{
return A();
}

int main()
{
func() = A(); //valid

}


So it is the same as an lvalue in that regard.

Share this post


Link to post
Share on other sites
Yes it can because it really invokes the operator = on the object, which is a regular function unlike the equal sign you use on ints. If you think about it that way it makes some sense. The bigger problem is that C++ mangles the concept of rvalue/lvalues which is very strict in C. Don''t lose sleep over this. C++ is a quirky language and this is one of the examples, so just understand how it works and stop thinking about why it works.

Share this post


Link to post
Share on other sites

  • Advertisement