Why are literals and temporary variables not lvalues?

  • A+
Category:Languages

I've read that lvalues are "things with a defined storage location".

And also that literals and temporaries variables are not lvalues, but no reason is given for this statement.

Is it because literals and temporary variables do not have defined storage location? If yes, then where do they reside if not in memory?

I suppose there is some significance to "defined" in "defined storage location", if there is (or is not) please let me know.

 


And also that literals and temporaries variables are not lvalues, but no reason is given for this statement.

This is true for all temporaries and literal except for string literals. Those are actually lvalues and is explained below.

Is it because literals and temporaries variables do not have defined storage location? If yes, then where do they reside if not in memory?

Yes. The literal 2 doesn't actually exist, it is just a value in the source code. Since it isn't an object, it's a value, it doesn't have to have any memory associated to it. It can be hard coded into the assembly that the compiler creates, or it could be put somewhere but since it doesn't have to all you can do is treat it as a pure value, not an object.

There is an exemption though and that is string literals. Those actually have storage since a string literal is an array of const char[N]. You can take the address of a string literal and a string literal can decay into a pointer, so it is an lvalue, even though it doesn't have a name.

Temporaries are also rvalues. Even if the object exists, its storage location is ephemeral. It only last until the end of the full expression they are in. You are not allowed to take their address and they also do not have a name. They might not even exist, for instance, in

Foo a = Foo(); 

The Foo() can be removed and the code semantically transformed to

Foo a(); // you can't actually do this since it declares a function by that is the semantics it has. 

so now there isn't even a temporary object in the optimized code.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: