13

考虑以下代码:

int&& x = 42;
static_assert(std::is_same<decltype( x ), int&&>::value, "&&");
static_assert(std::is_same<decltype((x)), int& >::value, "&" );

那么,什么是类型x?是一个int&&还是一个int&

(读完这个答案后,我问自己这个问题。)

4

1 回答 1

14

x(变量)的类型是int&&。所以decltype(x)产量int&&。表达式的类型xint。如果表达式是左值,则decltype((x))产生对表达式类型的左值引用。所以decltype((x))产量int&

于 2011-07-28T18:40:46.250 回答