4 ms·
lvalues are locatable, they represent an object that occupies some identifiable location in memory (has address). rvalues do not and therefore cannot be address
by somedudeatwork 8y ago
lvalues are locatable, they represent an object that occupies some identifiable location in memory (has address). rvalues do not and therefore cannot be addressed (with & or *)
I think of lvalues as things on the stack and rvalues as things stored in registers
https://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c/ https://eli.thegreenplace.net/2011/12/15/understanding-lvalu...
- dbaupp 8y agoThe article starts by explaining lvalues and rvalues more precisely than that, and breaks them up into 3 additional value categories in modern C++ (the ones in the title). (Also, lvalues aren't tied to the stack: they can be anywhere in memory, like heap or static.)
- leetcrew 8y agohow can you have an lvalue stored in the heap? isn't the lvalue itself a pointer in the stack? obviously I see how it can have static storage.
- dbaupp 8y agoIf foo is an lvalue, then I believe (for example) *(foo[0].bar->baz) is also an lvalue, and those pointer dereferences can go anywhere. Those values occupy a location in memory, as you said, even if it isn't on the stack.
- leetcrew 8y agooh okay, understood. frankly I don't have an airtight understanding of the c++ standards and I would have considered your expression to be a dereference operation on an lvalue, but not an lvalue itself. ty for the explanation!
- simiones 8y agoI'm sure there are some complex exceptions, but as a starter, you can think of lvalues as "anything you can assign to" (the l basically stands for left, as in, "can appear on the left of an assignment"). So, if you have something like `foo(val) = 10`, then `foo(val)` is an lvalue.
- mannykannot 8y agoOne of the complications is that if you take an l-value and const-qualify it, it is still an l-value (but not a modifiable l-value.) https://www.geeksforgeeks.org/lvalue-and-rvalue-in-c-language/ https://www.geeksforgeeks.org/lvalue-and-rvalue-in-c-languag...
- mannykannot 8y agoThey are not mutually exclusive. When a pointer has been set to point to an lvalue, dereferencing it yields that lvalue.
- gumby 8y agoglobal variables are lvalues, for instance.
- jstimpfle 8y agoC99 actually lets you make "pointer literals" (dont know the proper name) by putting an addressof operator in front of rvalue expressions. I cant offer a better explanation than the one you gave, though.
- quietbritishjim 8y ago> rvalues do not and therefore cannot be addressed (with & or *) Correction: prvalues cannot be addressed. There are also xvalues, which are both rvalues and lvalues i.e. they can be addressed but can also be moved from. In practice I think these are always lvalues that have been cast to an rvalue reference, usually using std::move().
- mannykannot 8y agoThis thread is recapitulating the article.
- twic 8y agoI used to think that. It's simple, right? Then i said so on the C++ Slack and got the hell beaten out of me. In modern C++, lvalue and rvalue simply don't mean what they do in other languages. Wild but true.
- ncmncm 8y agoLvalue and rvalue are inventions that serve a particular purpose in Algol-family languages. They are not physics. These other value categories are further inventions for additional purposes. The only unfortunate bit is the insistence on one-letter abbreviations when discussing them.