Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glvalue real examples and explanation?

Tags:

c++

c++11

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.

like image 525
AnArrayOfFunctions Avatar asked Feb 12 '23 09:02

AnArrayOfFunctions


1 Answers

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.

like image 177
Columbo Avatar answered Feb 22 '23 13:02

Columbo