According to this answer, ref
should be an int
.
But for some reason it evaluated to int&
, both in gcc and MSVC2015, while decltype(b)
is correctly evaluated to just int
. Why so?
int a = 1, b = 2;
decltype(a, b) ref; // ref is int&
decltype(b) var; // var is int
a, b
is an expression. According to decltype
rules for expressions, if result of the expression is an lvalue, type is going to be deduced as T&
7.1.6.2/4 Simple type specifiers [dcl.type.simple]
For an expression e, the type denoted by decltype(e) is defined as follows:
- if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;
- otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;
- otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
- otherwise, decltype(e) is the type of e.
The confusing part about difference between "type of the entity named by e
" and "type of e
" is easy to understand with example:
If some entity e
is declared as int& e = x;
, then later, in expression e
, type of e
is int
, and type of the entity named by e
is int&
. In short, type of e
drops reference qualifiers.