我惊讶地发现对于某些人来说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
cppreference似乎并不表明此错误是预期的。
还有其他类型需要 declval<T>
不能使用?规范在哪里定义这些?
最佳答案
每[declval] ,declval
的签名是:
template <class T>
add_rvalue_reference_t<T> declval() noexcept;
因此,如果 add_rvalue_reference_t<T>
,则调用格式不正确。不能作为返回类型说明符出现。
合格的函数类型有一个特殊的规则:
A function type with a cv-qualifier-seq or a ref-qualifier (including a type named by typedef-name ([dcl.typedef], [temp.param])) shall appear only as:
(6.1) the function type for a non-static member function,
(6.2) the function type to which a pointer to member refers,
(6.3) the top-level function type of a function typedef declaration or alias-declaration,
(6.4) the type-id in the default argument of a type-parameter, or
(6.5) the type-id of a template-argument for a type-parameter ([temp.arg.type]).
它们不能是返回类型说明符。
浏览Types ,我很确定限定函数类型是唯一的情况。
关于c++ - 对于什么 T `std::declval<T>()` 没有匹配函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58256787/