decltype and parentheses
Solution 1:
Just above that example, it says
- if e is an unparenthesized id-expression or a class member access (5.2.5), decltype(e) is the type of the entity named by e.
- if e is an lvalue, decltype(e) is T&, where T is the type of e;
I think decltype(a->x)
is an example of the "class member access" and decltype((a->x))
is an example of lvalue.
Solution 2:
decltype(a->x)
This gives you the type of the member variable A::x
, which is double
.
decltype((a->x))
This gives you the type of the expression (a->x)
, which is an lvalue expression (hence why it is a const reference--a
is a const A*
).
Solution 3:
The added parens are turning it into a lvalue.
MSDN says
The inner parentheses cause the statement to be evaluated as an expression instead of a member access. And because a is declared as a const pointer, the type is a reference to const double.