命名右值引用的类型是什么?

What is the type of a named rvalue reference?

本文关键字:类型 是什么 引用      更新时间:2023-10-16

考虑以下代码:

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

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

(看完这个答案后我问了自己这个问题)

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