c++c++11boostdecltypexvalue

Is this expression an xvalue?


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?


Solution

  • 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).