-1

Are all xvalues glvalues and rvalues at the same time? Or a xvalue may be either glvalue or a rvalue?

If it's a glvalue or/xor rvalue, can you give a example for each case?

João Paulo
  • 6,300
  • 4
  • 51
  • 80

2 Answers2

3

I think the key to understanding value categories is to understand the difference between a value and an object. A value is just an abstract instance of a type, one of all the possible bit patterns that makes up the information associated with an instance of a particular type. An object, on the other hand, is a particular piece of storage set aside at a particular location in memory in such a way that a value of a particular type can be stored in that location. We say the object holds a value of that type.

Think of the whole prvalue, xvalue, lvalue business like this: A prvalue is just a value of a given type, it is not necessarily stored anywhere. It really just is a value of a given type, existing only for an instant, at the one point in the particular expression in which it occurs. An lvalue, on the other hand, identifies a particular object in which a value of a particular type is stored. The object has a certain, well-defined time during which it is alive and keeps whatever value is stored inside of it. An lvalue is not a value of a particular type itself, it identifies an object that holds a value of a particular type. Thus, prvalue and lvalue sort of sit at two opposite ends on a spectrum, a prvalue being just a value in no particular storage with no persistence while an lvalue identifies a particular piece of storage persistently holding some value. An xvalue is something in between. An xvalue identifies a particular object at a particular location, the value stored in which, however, is not needed anymore. In that sense, an xvalue shares properties with both, lvalues and prvalues: it identifies an object at a particular location, but the value stored in that object does not have persistence:

         explicit storage location    persistence
prvalue           no                      no
xvalue           yes                      no
lvalue           yes                     yes

What prvalue and xvalue have in common is the lack of persistence, which is the defining characteristic of a general rvalue. What xvalue and lvalue have in common is the particular storage location, which is the defining characteristic of a general glvalue. An xvalue is just an lvalue that has no persistence and, thus, can be treated like an rvalue…

Michael Kenzel
  • 15,508
  • 2
  • 30
  • 39
1

From cppreference:

An rvalue expression is either prvalue or xvalue.

an xvalue (an “eXpiring” value) is a glvalue that denotes an object or bit-field whose resources can be reused;

Answering your questions:

Are all xvalues glvalues and rvalues at the same time?

Yes. More specifically, rvalue is a superset of prvalue and xvalue. Thus all xvalues are rvalues, but not all rvalues are xvalues (namely, the ones that are prvalues). And by the definition above, xvalue is a reusable glvalue. So xvalues are both glvalues and rvalues.

Or a xvalue may be either glvalue or a rvalue?

No. xvalue is purely a reusable glvalue, and reusable glvalues (i.e. xvalues) are rvalues. Think move semantics.

Examples:

int a = 6;
foo(6); // 6 is a prvalue (and thus is an rvalue)
foo(a); // a is a glvalue

std::vector<int> vec = {1, 2, 3};
bar(vec);            // vec is a glvalue
bar(std::move(vec)); // std::move(vec) is an xvalue (reusable glvalue)
                     // (and thus is an rvalue)
Community
  • 1
  • 1
Cruz Jean
  • 2,761
  • 12
  • 16
  • Sorry, if I didn't get it quickly, but in your example `bar(std::move(vec));`, you said it's a reusable glvalue and thus a rvalue, then the expression is both rvalue and glvalue? – João Paulo Apr 30 '19 at 21:30
  • @JoãoPaulo Yes, but rvalue isn't really a thing in an of itself. It's just anything that happens to be prvalue or xvalue. They're collectively referred to as rvalues, but that's just a matter of naming conventions. – Cruz Jean Apr 30 '19 at 21:32
  • So it's glvalue and xvalue at the same time, no? – João Paulo Apr 30 '19 at 21:34
  • @JoãoPaulo Yes, because all xvalues are technically glvalues (which are reusable). – Cruz Jean Apr 30 '19 at 21:39
  • 2
    A good explanation, except I would answer "yes" to "Are all xvalues glvalues and rvalues at the same time?" Every expression has exactly one specific value category, which is either lvalue, xvalue, or prvalue. "Glvalue" is just a way of saying "either lvalue or xvalue"; "rvalue" is a way of saying "either xvalue or prvalue". So every xvalue is also a glvalue and an rvalue (though time has nothing to do with it). There are certainly properties that apply to glvalues and rvalues and useful ways of thinking of them (hence the shortcuts), but that doesn't change what an expression is and isn't. – aschepler Apr 30 '19 at 22:23
  • @aschepler Ah, thanks for catching that - fixed. – Cruz Jean Apr 30 '19 at 22:36