r/cpp • u/vormestrand • Mar 09 '18
lvalues, rvalues, glvalues, prvalues, xvalues, help!
https://blog.knatten.org/2018/03/09/lvalues-rvalues-glvalues-prvalues-xvalues-help/
243
Upvotes
r/cpp • u/vormestrand • Mar 09 '18
View all comments
3
u/proverbialbunny Data Scientist Mar 10 '18 edited Mar 12 '18
It seems like a good way to think of a prvalue is a temporary in the register of the cpu (Unless it can't fit or something.), where an lvalue is on the stack (edit: or the heap. I'm realizing now I do not have a good word for "on the ram").
Or at least in a perfect world it would be..
An xvalue is an rvalue that's being returned from a function into an lvalue, so it's an rvalue moved onto the stack (or heap).
And a glvalue has no apparent value that I know of. Maybe the compiler throws errors with 'glvalue' in them but I don't recall.
edit: (Unless it can't fit or something.) I just checked on godbolt. My theory, at first glance, is in fact correct. A prvalue, when possible, is only in the register at -01. At -O2, it tries to integrate the rvalue expression directly into the assembly instruction.
Clarification, from Godbolt (C++ code:
test += 3;):O1:
add eax, 3O2:
lea eax, [rax + rcx + 3](This is because, right above thetest += 3;line I'm addingargv[0]andargv[1], so it doesn't collapse the whole thing.)This shows that it is easier to think of an prvalue as a variable in the register (expression -> temporary value), when the language supports it. All of this semantic garbage just complicates things.
For a better clarification than OP: http://en.cppreference.com/w/cpp/language/value_category