Search code examples
c++c++11lvaluedecltype

decltype and lvalue expression


according to http://en.cppreference.com/w/cpp/language/decltype

struct A {
    double x;
};
const A* a = new A();

decltype( a->x ) x3;

match 1 case, i.e:

If the argument is either the unparenthesised name of an object/function, or is a member access expression (object.member or pointer->member), then the decltype specifies the declared type of the entity specified by this expression.

but this note:

if the name of an object is parenthesised, it becomes an lvalue expression

lead me to the following question: what is the type of a->x if it is not a lvalue expression?

I don't even get why

decltype((a->x)) x4 = x3; // type of x4 is const double& (lvalue expression)

is evaluated as a const& only by the fact that it is considered as a lvalue expression, don't really see the link.


Solution

  • lead me to the following question what is the type of a->x if it is not a lvalue expression ?

    You are simply confused.

    if the name of an object is parenthesised, it becomes an lvalue expression

    That should be

    if the name of an object is parenthesized, it is handled different by decltype.

    Both the parenthesized and nonparenthesized things are lvalue expressions. If you don't parenthesize, then decltype does not inspect the type of the expression, but the type used by the declaration that the name lookup found the name to refer to (for example, it could be int&& if name lookup resolves it to an rvalue reference variable, but the type of the expression is int and is an lvalue).