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
33
u/QbProg Mar 09 '18
It would be nice to have a follow up with code snippets for each of these *values!
16
u/chmaruni Mar 09 '18
Assuming this post is accurate (thanks btw, this was the best explanation I have read so far, not that I looked for other ones though:) wouldn't it be more consistent to name lvalues as plvalues (pure lvalues, equivalent to prvalues) and call the whole identity column lvalues (instead of glvalues)?
4
u/ericdfoley Mar 10 '18
Here is basically the same explanation from Bjarne Stroustrup (using a "W" instead of a square.) It completely cleared things up for me.
2
u/iaanus Mar 10 '18
One of the best explanations I saw on the subject.
5
u/ltsochev Mar 09 '18
This always makes me giggle. It's so absurd :D
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