我惊讶地发现,对某些T来说,decltype(std::declval<T>())是不合法的:
#include <utility>
template<typename T>
using Alias = decltype(std::declval<T>());
// as expected
using A1 = Alias<int>;
using A2 = Alias<int(int)>;
// error: no matching function for call to 'declval<...>()'
using A3 = Alias<int(int) const>;
using A4 = Alias<int(int) volatile>;
using A5 = Alias<int(int) &>;
using A6 = Alias<int(int) &&>;
// and all combinations of the above优先选择似乎并不表示预期会出现此错误。
还有其他不能使用declval<T>的类型吗?规范在哪里定义这些?
发布于 2019-10-06 11:56:58
根据[解密],declval的签名是:
template <class T>
add_rvalue_reference_t<T> declval() noexcept;因此,如果add_rvalue_reference_t<T>不能作为返回类型说明符出现,则调用的格式不正确.
限定函数类型有一条特殊规则:
带有cv-限定符-seq或ref-限定符的函数类型(包括由ty胡枝子名称命名的类型(dcl.typedef,temp.param))只应显示为:
它们不能是返回类型说明符。
纵观类型,我很确定限定的函数类型是唯一的情况。
https://stackoverflow.com/questions/58256787
复制相似问题