Is this expression an xvalue?
Asked Answered
G

1

6

The C++ standard says the following about 'xvalues' (N4762 § 7.2.1.4):

An expression is an xvalue if it is:
- . . .
- a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue

Consider the following code fragment (using Boost to print the type of an expression):

#include <iostream>
#include <boost/type_index.hpp>

using boost::typeindex::type_id_with_cvr;

struct X {
    int var;
} x;

int main()
{
    auto extended_type = type_id_with_cvr<decltype( std::move(x).var )>();
    std::cout << extended_type.pretty_name() << std::endl;
}

My question is about the expression std::move(x).var:

Based on the text in the standard, I expect the expression to be an xvalue, but the output is int, not int &&.

What am I missing here?

Gaskins answered 26/8, 2018 at 22:47 Comment(0)
T
11

My question is about the expression: std::move(x).var

Based on the text in the standard, I expect the expression to be an xvalue,

It is.

but the output is int, not int &&

That's because decltype comes in two forms. It can give information on how a name is declared, or it can either give information on the type and category of the expression.

Since std::move(x).var is a member access, you get the former. To get the latter, use decltype((std::move(x).var)) (with double parentheses).

Trottier answered 26/8, 2018 at 23:2 Comment(2)
Thanks, that makes perfect sense. Glad I wasn't wrong about the expression typeGaskins
So this is an actual use case for decltype with double parentheses!Maniac

© 2022 - 2024 — McMap. All rights reserved.