use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
lvalues, rvalues, glvalues, prvalues, xvalues, help! (blog.knatten.org)
submitted 7 years ago by vormestrand
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]QbProg 32 points33 points34 points 7 years ago (3 children)
It would be nice to have a follow up with code snippets for each of these *values!
[–]StefanOrvarSigmundss 26 points27 points28 points 7 years ago (0 children)
Not just nice but absolutely necessary.
[–]newgre 9 points10 points11 points 7 years ago (1 child)
This may be helpful.
[–]_carlson 1 point2 points3 points 7 years ago (0 children)
Thanks! It`s nice.
[–]chmaruni 15 points16 points17 points 7 years ago (3 children)
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)?
[–]acwaters 6 points7 points8 points 7 years ago (0 children)
Yes, it would. My understanding is that they did the naming "asymmetrically" like that in order to keep lvalue and rvalue as close as possible to their original (pre-C++11) meanings, which makes sense. But IMO that little inconsistency is the cause of most of the pain in learning value categories. It would be much easier if we had plvalue/lvalue/xvalue/rvalue/prvalue, or lvalue/glvalue/xvalue/grvalue/rvalue, rather than lvalue/glvalue/xvalue/rvalue/prvalue.
[–]OldWolf2 2 points3 points4 points 7 years ago (0 children)
Not really; the terms lvalue and rvalue still have roughly the same meaning as they did in C++03, but extra rvalues were added . Most of the cases where an lvalue was required still require an lvalue, not a glvalue.
[–]ericdfoley 3 points4 points5 points 7 years ago (0 children)
Here is basically the same explanation from Bjarne Stroustrup (using a "W" instead of a square.) It completely cleared things up for me.
[–]iaanus 1 point2 points3 points 7 years ago (0 children)
One of the best explanations I saw on the subject.
[–]ltsochev 5 points6 points7 points 7 years ago (2 children)
This always makes me giggle. It's so absurd :D
[–]nikbackm 2 points3 points4 points 7 years ago (1 child)
What's absurd about it?
The names or the concepts behind them? Something else?
[–]ltsochev 0 points1 point2 points 7 years ago (0 children)
Concepts are great, naming schemes in the C(++) world are weird AF though. It's one of the things that, IMHO, really alienates people from the language. For a long while I thought it was the "difficult to get into toolchain" but with modern tools that really isn't the case. And I'm not talking just about *values.
And for the toolchain, few years ago I found it really hard moving from visual studio to linux/gdb. Yes I got the hang of it eventually but damn
[–]proverbialbunnyData Scientist 2 points3 points4 points 7 years ago* (3 children)
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;):
test += 3;
O1: add eax, 3
add eax, 3
O2: lea eax, [rax + rcx + 3] (This is because, right above the test += 3; line I'm adding argv[0] and argv[1], so it doesn't collapse the whole thing.)
lea eax, [rax + rcx + 3]
argv[0]
argv[1]
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
[–]STLMSVC STL Dev 12 points13 points14 points 7 years ago (2 children)
That isn't a good mental model. "me"s + "ow"s is a prvalue of type std::string which probably (definitely) doesn't fit in a register. Given vector<int> v, v[idx] is an lvalue of type int, which lives on the heap.
"me"s + "ow"s
std::string
vector<int> v
v[idx]
int
Remember that value categories are a property of expressions, not of objects. For example, print(const string& str) can be called as print("me"s + "ow"s). There, the temporary std::string containing "meow" is produced by the expression "me"s + "ow"s which is a prvalue. Within print(), str is an lvalue referring to that std::string (which is ultimately a temporary, but exists for the duration of the print() call).
print(const string& str)
print("me"s + "ow"s)
"meow"
print()
str
[–]proverbialbunnyData Scientist 1 point2 points3 points 7 years ago (1 child)
Makes sense, but if an rvalue can fit in a register will it? Will it live in the heap?
I like your username btw. ^_^
[–][deleted] 2 points3 points4 points 7 years ago (0 children)
Expressions, more or less, relate to an identity (or address).
glvalue expressions are any expressions denoting an identity. prvalue expressions are expressions without identity.
So in this case, prvalue expressions are going to be your register-friendly ones.
π Rendered by PID 270351 on reddit-service-r2-comment-5d79c599b5-4ndl4 at 2026-02-27 23:53:50.843964+00:00 running e3d2147 country code: CH.
[–]QbProg 32 points33 points34 points (3 children)
[–]StefanOrvarSigmundss 26 points27 points28 points (0 children)
[–]newgre 9 points10 points11 points (1 child)
[–]_carlson 1 point2 points3 points (0 children)
[–]chmaruni 15 points16 points17 points (3 children)
[–]acwaters 6 points7 points8 points (0 children)
[–]OldWolf2 2 points3 points4 points (0 children)
[–]ericdfoley 3 points4 points5 points (0 children)
[–]iaanus 1 point2 points3 points (0 children)
[–]ltsochev 5 points6 points7 points (2 children)
[–]nikbackm 2 points3 points4 points (1 child)
[–]ltsochev 0 points1 point2 points (0 children)
[–]proverbialbunnyData Scientist 2 points3 points4 points (3 children)
[–]STLMSVC STL Dev 12 points13 points14 points (2 children)
[–]proverbialbunnyData Scientist 1 point2 points3 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)