c++ - 对于什么 T `std::declval<T>()` 没有匹配函数?

标签 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

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/

相关文章:

c++ - 将枚举值传递给非标准的 int 参数吗?

c++ - "typename qualified-id"引用非类型参数声明中的类型

perl - 双变量的真实性是否总是其字符串部分的真实性?

c++ - 我可以使用 declval 来构造一个未使用的返回值吗?

c++ - 为什么libstdc++-v3中declval的实现看起来那么复杂?

适用于 iOS 的 Objective-C 中的 C++ 代码

c++ - std::unordered_map::find 使用与 Key 类型不同的类型?

c++ - (int32_t) 255 << 24 是 gcc (C++11) 中的未定义行为吗?

c++ - 函数指针是否有声明?

c++ - 使用已删除的函数 ‘sf::RenderWindow& sf::RenderWindow::operator=(const sf::RenderWindow&)’