std :: declval <T>()`没有匹配功能是什么原因?

Eri*_*ric 3 c++ language-lawyer declval

我很惊讶地发现,对于某些人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
Run Code Online (Sandbox Code Playgroud)

cppreference似乎并不表明该错误是预期的。

还有其他declval<T>不能使用的类型吗?规范在哪里定义这些?

L. *_* F. 5

[declval]的签名declval是:

template <class T>
add_rvalue_reference_t<T> declval() noexcept;
Run Code Online (Sandbox Code Playgroud)

因此,如果调用add_rvalue_reference_t<T>不能作为返回类型说明符出现,则调用格式错误。

合格的函数类型有一个特殊的规则:

具有cv-qualifier-seqref-qualifier的函数类型 (包括以typedef-name命名的类型([dcl.typedef],[temp.param]))应仅显示为:

  • (6.1)非静态成员函数的函数类型,

  • (6.2)指向成员的指针所指向的函数类型,

  • (6.3)函数typedef声明或别名声明的顶级函数类型,

  • (6.4)type-parameter的默认参数中的type-id,或

  • (6.5)类型参数([temp.arg.type])的模板参数的类型 ID。

它们不能是返回类型说明符。

查看Types,我很确定合格的函数类型是唯一的情况。