4

I know what 'xvalues', 'prvalues', 'rvalues' and 'lvalues' are, how they are helpful and I've seen real examples of them. But I've never understand what a 'glvalue' is, and how it co-operate with the others. I've searched everywhere but with no-luck even in the latest standard paper it was barely noticed. Can somebody explains it to me and show some examples?

Note that this is not a duplicate of this, as even there nobody gave an example of 'glvalue'. Here too. It was only barely mentioned like this:

A glvalue (“generalized” lvalue) is an lvalue or an xvalue.

Community
  • 1
  • 1
AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66
  • `int a = 4;` `a` is a lvalue, but it's also a glvalue AFAIK. – AndyG Dec 08 '14 at 18:50
  • 1
    I presume you've seen the [chart](http://i.stack.imgur.com/YKlod.png)? The value categories at the bottom of the chart are mutually exclusive. An lvalue is not an xvalue, but lvalues and xvalues are glvalues and so on. –  Dec 08 '14 at 18:55

2 Answers2

5

By definition from §3.10\1

A glvalue (“generalized” lvalue) is an lvalue or an xvalue

where

Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue.

Here the taxonomy:

enter image description here

So, for instance, every lvalue is a glvalue:

int x = 7; // x is an lvalue. x is also a glvalue.
           // 7 is a literal, so it is a prvalue. 7 is not a glvalue.

auto foo = static_cast<int&&>(x); // foo is an lvalue, so it is a glvalue
                                  // the cast is an rvalue but not a prvalue,
                                  // it is an xvalue. so it is a glvalue.
Barry
  • 286,269
  • 29
  • 621
  • 977
5

A glvalue is anything that isn't a prvalue. Examples are names of entities, or expressions that have reference type (regardless of the kind of the reference).

int i;
int* p = &i;
int& f();
int&& g();

int h();

h() // prvalue
g() // glvalue (xvalue)
f() // glvalue (lvalue)
i   // glvalue (lvalue)
*p  // glvalue (lvalue)

std::move(i)  // glvalue (xvalue)

As the quote in your question clearly states, the category glvalue includes all xvalues and lvalues. lvalues, xvalues and prvalues are complementary categories:

Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue.

You should be familiar with lvalues. Now consider what xvalues are, [expr]/6:

[ Note: An expression is an xvalue if it is:

  • the result of calling a function, whether implicitly or explicitly, whose return type is an rvalue reference to object type,
  • a cast to an rvalue reference to object type,
  • a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue, or
  • a .* pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.

[…] — end note ]

So, roughly speaking, you could think of glvalues as
"All lvalues plus expressions involving rvalue references".
We use it to describe expressions that refer to objects rather than "being" those objects.

Columbo
  • 60,038
  • 8
  • 155
  • 203
  • This answer is being discussed on Stack Overflow: https://stackoverflow.com/q/44848970/560648 – Lightness Races in Orbit Jun 30 '17 at 15:02
  • @Columbo `We use it to describe expressions that refer to objects rather than "being" those objects.` - You said that `i` is `glvalue` and here you say that `glvalue` is an expression who refer to an object. Can you clarify this? – Stav Alfi Dec 24 '17 at 18:05