§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 typedeftype
shall nameT&&
; otherwise,type
shall nameT
. [ Note: This rule reflects the semantics of reference collapsing (8.3.2). For example, when a typeT
names a typeT1&
, the typeadd_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/