c++ - 对于什么 T `std::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>
因此,如果不能作为返回类型说明符出现,则该调用是格式错误的。
合格的函数类型有一个特殊的规则:
具有cv-qualifier-seq或ref-qualifier的函数类型 (包括由typedef-name ([dcl.typedef], [temp.param]) 命名的类型)应仅显示为:
(6.1) 非静态成员函数的函数类型,
(6.2) 指向成员的指针所指的函数类型,
(6.3) 函数 typedef 声明或alias-declaration的顶级函数类型,
(6.4) type-parameter的默认参数中的type-id,或
(6.5)类型参数([temp.arg.type])的模板参数的类型ID 。
它们不能是返回类型说明符。
查看Types,我很确定合格的函数类型是唯一的情况。
推荐阅读
- javascript - 电容相机不显示提示对话
- c# - 使用多重绑定设置 c# wpf 组合框背景颜色
- html - 如何在 CSS 和 HTML 中复制此页脚?
- oracle-cloud-infrastructure - 在 Oracle Cloud 中打开特定端口 - Ubuntu 18
- bash - 用户如何在bash中传递数组的特定索引中的值
- postgresql - DBevear 不显示详细的数据库视图
- vb.net - *.Designer.vb 中的属性更改未更新
- ubuntu - 使用 iconv 命令将 UTF-8 (US-ASCII) 转换为 ISO8859-1
- r - R 中的 %...!% 和 %...>% 有什么区别?
- tensorflow - 在多类分类中从 tensorflow 2.3.1 降级到 tensorflow 1.14 或 1.15 时,过拟合会降低准确度性能