c++ - 为什么 'declval' 指定为 'add_rvalue_reference<T>::type' 而不是 'T&&' ?

标签 c++ c++11

§20.2.4 [declval]

template <class T>
typename add_rvalue_reference<T>::type declval() noexcept; // as unevaluated operand

为什么使用 add_rvalue_reference在这里?

来自 §20.9.7.2 [meta.trans.ref]add_rvalue_reference :

If T names an object or function type then the member typedef type shall name T&&; otherwise, type shall name T. [ Note: This rule reflects the semantics of reference collapsing (8.3.2). For example, when a type T names a type T1&, the type add_rvalue_reference<T>::type is not an rvalue reference. —end note ]

自从 add_rvalue_reference无论如何都是为了反射(reflect)引用崩溃,为什么不直接使用 T&&像下面这样?

template<class T>
T&& declval();

会出什么问题?这两个版本到底有什么区别?

最佳答案

我不知道这是否是真正的原因,但是 add_rvalue_reference void 有不同的行为.

add_rvalue_reference<void>::type简直是void .

void&&是一个错误。

关于c++ - 为什么 'declval' 指定为 'add_rvalue_reference<T>::type' 而不是 'T&&' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8955052/

相关文章:

c++ - 为什么访问基类指针 vector 中的派生类成员会返回错误?

c++ - std::hash<std::string> 可以为不同的字符串返回相同的值吗?

C++ move 具有整数成员的对象的语义

c++ - 在旧版本中使用新的 c++11 功能

c++ - 如何将L值传递给std::make_pair

C++ 放大器 : Transferring data async and keeping data on accelerator

c++ - 单击按钮后,Wt C++ 使窗口不可单击

C++ OpenGL 着色版本错误 - 不支持 GLSL x [Ubuntu 16.04]

c++ - 从整数索引 for 循环枚举到范围 for

c++ - 如何修复 eigen3 中的 'non-type template argument is not a constant expression'?