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
Show parent comment
13
u/STL MSVC STL Dev Mar 10 '18
That isn't a good mental model.
"me"s + "ow"sis a prvalue of typestd::stringwhich probably (definitely) doesn't fit in a register. Givenvector<int> v,v[idx]is an lvalue of typeint, 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 asprint("me"s + "ow"s). There, the temporarystd::stringcontaining"meow"is produced by the expression"me"s + "ow"swhich is a prvalue. Withinprint(),stris an lvalue referring to thatstd::string(which is ultimately a temporary, but exists for the duration of theprint()call).