- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个简单的模板化包装器结构,其成员函数在其模板类型的对象上调用 .error()
。
template <typename T>
struct Wrapper {
T t;
decltype(auto) f() {
return t.error(); // calls .error()
}
};
如果我用一个没有 error()
成员函数的类型实例化它,只要我不调用它就没问题。这就是我想要的行为。
Wrapper<int> w; // no problem here
// w.error(); // uncommented causes compilation failure
如果我使用我认为是具有尾随返回类型的语义等价物,它会在变量声明上出错
template <typename T>
struct Wrapper {
T t;
auto f() -> decltype(t.error()) {
return t.error();
}
};
Wrapper<int> w; // error here
我承认这两者在语义上并不等价,但是无论如何可以使用尾随返回类型(仅限 C++11)来获得前者的行为,而不需要使用某种 来专门化整个类HasError
tmp 欺骗?
最佳答案
版本之间的区别
decltype(auto) f();
auto f() -> decltype(t.error());
是第二个函数声明可以无效。函数模板的返回类型推导发生在定义被实例化[dcl.spec.auto]/12时。虽然我找不到任何有关类模板成员函数的返回类型推导的信息,但我认为它们的行为相似。
隐式实例化类模板 Wrapper
会导致实例化声明,但不会实例化所有(非虚拟)的定义成员函数[temp.inst]/1。声明 decltype(auto) f();
有一个非推导的占位符但有效。另一方面,auto f() -> decltype(t.error());
对于某些实例化有无效的返回类型。
C++11 中的一个简单解决方案是推迟确定返回类型,例如通过将 f
转换为函数模板:
template<typename U = T>
auto f() -> decltype( std::declval<U&>().error() );
不过,该函数的定义让我有点担心:
template<typename U = T>
auto f() -> decltype( std::declval<U&>().error() )
{
return t.error();
}
对于 Wrapper
的特殊化,其中 t.error()
无效,上面的 f
是一个函数模板,不能产生有效的专长。这可能属于 [temp.res]/8,它表示此类模板格式错误,无需诊断:
If no valid specialization can be generated for a template, and that template is not instantiated, the template is ill-formed, no diagnostic required.
但是,我怀疑该规则已引入允许,但不要求实现检查非实例化模板中的错误。在这种情况下,源代码中没有编程错误;该错误将发生在源代码描述的类模板的实例化中。所以,我觉得应该没问题。
另一种解决方案是使用后备返回类型使函数声明格式正确,即使定义不是(对于所有实例化):
#include <type_traits>
template<typename T> struct type_is { using type = T; };
template <typename T>
struct Wrapper {
T t;
template<typename U=T, typename=void>
struct error_return_type_or_void : type_is<void> {};
template<typename U>
struct error_return_type_or_void
<U, decltype(std::declval<U&>().error(), void())>
: type_is<decltype(std::declval<U&>().error())> {};
auto f() -> typename error_return_type_or_void<>::type {
return t.error();
}
};
关于c++ - 成员函数中的 decltype(auto) 忽略无效主体,decltype(expr) 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29463711/
这个问题在这里已经有了答案: Difference between JSP EL, JSF EL and Unified EL [closed] (2 个回答) 5年前关闭。 前段时间我读过关于“Co
在阅读德州仪器为其 SensorTag 提供的示例代码时我发现了以下代码段。 void SensorTagIO_processCharChangeEvt(uint8_t paramID) {
我正在尝试编写支持简单语法的宏,例如: boo: 3 and foo: go or bar: 4+2 这是我试过的: macro_rules! solr { ($TOPIC:ident : $
有人知道 C 中 if(Expr,Expr) 结构的逻辑含义是什么吗?喜欢: if(lcn = (ULONG)GetLCN(index),lcn) 最佳答案 当您有“,”时,表达式的计算结果为列表中的
我花了几个小时试图让下面的代码工作 PATH="C:\Ben\MyPictures" echo $PATH MY=`expr 2 + 2` 但这不会起作用,因为“expr: c
我尝试将两个数字(实际上这些是某些远程执行命令的输出)分配给 2 个不同的变量,比如说 A 和 B。 当我回显 A 和 B 时,它们显示值: echo $A 809189640755 echo $B
我该怎么做才能使以下工作正常进行? 我需要创建一个接受 Expr 列表并返回 Expr 的函数(Expr 列表 -> Epxr)。 type DataObject() = let data =
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: bash: $[] vs. $(()) $(( expr )) 构造可用于整数 math在 bash 中,例
我很难理解这里的声明: for( int i=0; i< out_length; i++){ int num=i < length_a ? array_a[i] : 0; ... ... 我
我只是好奇。我想知道表达式是否有特定原因 var &= expr 行为方式与不同 var = var && expr. 看起来第一个表达式中的表达式正在执行,而不管 var 上的 false 值。 我
在 bash 中,我不明白为什么第三个命令不正确: [[ 1 -eq 1 ]] # $? is 0 [[ ! 1 -eq 1 ]] # $? is 1 [[ ! ! 1 -
这个问题在这里已经有了答案: Lifetime of temporaries (2 个答案) 关闭 8 年前。 我有一个统计管理器,它通过测量方法执行所需的时间来检查我的应用程序性能。它的用法是这样
我不确定这里是否有人问过这个问题,但我在这里很困惑。我正在读这本很棒的书,名为《Advanced R》,作者为 Hadley Wickham,来自 here . 这里已经描述了一个名为cement的函
我正在通过解析类似 Excel 的公式来生成 sql 请求。 因此,对于给定的公式,我收到以下请求: SELECT IF(COL1='Y', SUM(EXPR),NULL) FROM Table 我没
我看到了一些非常奇怪的东西,我无法解释。我在猜测一些我不熟悉的 C# 边缘情况,或者运行时/发射器中的错误? 我有以下方法: public static bool HistoryMessageExis
我在玩Prefix和 Postfix运算符(分别为 @ 和 //),我遇到了以下问题。 给定以下代码,它们以相同的方式进行评估: Hold[MatrixPlot@Sort@data] // FullF
有没有人在使用 Pandas 时解决了这个 pylint 问题? C:525,59: Comparison to True should be just 'expr' or 'expr is True
我正在尝试制作一个可以按以下方式调用的宏: mactest!(some::Path[1, 2, AnotherName[3, 4]]) 这相当于以下内容: make_result( "some
这两种方法有区别吗? public String toString() { return this.from.toString() + this.to.toString(); } public
我有一个小问题。如果你想测试这里的两个 bash 脚本。这是工作 #!/bin/bash N=40 while [ "$N" -gt 0 ] do echo $N N=`expr $N - 1` do
我是一名优秀的程序员,十分优秀!