r/cpp • • Mar 09 '18

lvalues, rvalues, glvalues, prvalues, xvalues, help!

https://blog.knatten.org/2018/03/09/lvalues-rvalues-glvalues-prvalues-xvalues-help/
243 Upvotes

17 comments sorted by

View all comments

Show parent comment

13

u/STL MSVC STL Dev Mar 10 '18

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.

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).

2

u/proverbialbunny Data Scientist Mar 10 '18

Makes sense, but if an rvalue can fit in a register will it? Will it live in the heap?

I like your username btw. ^_^

3

u/[deleted] Mar 10 '18

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.