user8221510
user8221510

Reputation:

Aren't xvalues both glvalues and rvalues?

I read this answer the part that caught my attention was this:

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)

and with this chart in mind

Expression type chart

I got confused.

If a glvalue is either an lvalue or an xvalue and an rvalue is either a prvalue or an xvalue, isn't it wrong to say that g() is a glvalue without saying that it is also an rvalue? The same with std::move(i).

What my version of the above code would look like:

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

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

As for what the standard says(I only quote the most related parts):

An rvalue (so-called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue

and

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

Of course, I could be wrong. If so, please help me out a little :)

Upvotes: 10

Views: 512

Answers (2)

wally
wally

Reputation: 11012

As I understand it from here

  • glvalue == has identity and
  • rvalue == can be moved from.

So your analysis is correct because an xvalue

  • can be moved from and
  • has identity.

Upvotes: 3

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385264

If a glvalue is either an lvalue or an xvalue and an rvalue is either a prvalue or an xvalue, isn't it wrong to say that g() is a glvalue without saying that it is also an rvalue?

Yes.

If g() is an xvalue, then it is both a glvalue and an rvalue.

Upvotes: 6

Related Questions