- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
考虑以下类:
struct S {
template<typename T>
std::enable_if_t<std::is_void<T>::value>
f() noexcept {}
template<typename T>
std::enable_if_t<not std::is_void<T>::value>
g() noexcept {}
};
正如预期的那样,编译:
s.f<void>();
这个不是:
s.g<void>();
令我困惑的是,下面的 main
是用 GCC (6.2) 编译的,而不是用 clang (3.9) 编译的:
int main() {
static_assert(noexcept(&S::f<void>), "!");
static_assert(noexcept(&S::g<void>), "!");
}
我会说第二个断言失败是因为特化无效。两个编译器不同意这一点。
哪个是正确的?
最佳答案
The set of potential exceptions of an expression
e
is empty ife
is a core constant expression (5.20).
也就是说,GCC 甚至不解析template-id,因为它从一开始就知道结果是true
。 (因为 g<void>
不是静态数据成员模板特化,其类型重载了 operator&
)。虽然很聪明,但这种行为是不符合规范的,因为任何 template-id 的出现都需要将参数替换到函数模板的声明中。
关于c++ - noexcept 运算符和 enable_if_t : do they work together?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141702/
使用 typename = enable_if_t 有什么区别吗?和 enable_if_t = true对于 SFINAE?我特别问,因为我偶然发现了一个看起来像错误的东西:Compiler err
我有以下代码,我在其中使用折叠表达式来评估所有包参数是否都可以转换为第一个函数参数。出于某种原因,当我进行看似非常微不足道的更改时,它无法在 msvc 上编译: #include #define T
我试图理解为什么这段代码没有按预期工作 #include #include #include using namespace std; struct Foo { }; template void
我正在尝试做一些持久性的东西,我有一个这样的结构: struct EntityPersistence { template void persist(Archive &ar, Ent
当我尝试使用 MSCL 19 编译以下代码时,没问题。但是,当我尝试 g++ 6.2.0 和 clang 3.9.0 时,出现了类似 expected nested-name-specifier cl
我想写一个 2 重载的模板组合,如下面的代码所示,我希望在 2 上做不同的处理对象的类型。编译时出现这些错误: // Error C2672 'info': no matching over
这是我的代码: template * = nullptr> void foo(T) { std::cout * = nullptr> void foo(T) {} // #2 class te
我需要在双引号内打印所有类型的字符串数据,而其他不带双引号的字符串数据。 这是我检查参数是否为字符串的函数。 template struct is_str : std::integral_consta
我有以下使用 std::enable_if 的案例: template::value>::type* = nullptr> void f() { } template::value>::type* =
我正在阅读std::enable_if ,并注意到以下内容。 template ::value, int> = 0 > T(Integer) : m_type(int_t) {} 由于 std::en
我编写了一个接收可变数量的 std::pairs 的函数,该函数从每对的第一个元素中减去第二个元素,并从新生成的结果中返回一个元组,如下所示: template, pairs>) && ...)>*
编辑:根据反馈重写了提供的代码以帮助说明问题 说我有一个方法 template do_foo(T)并且我纯粹使用 std::enable_if 来使用它定义返回值 这允许我为它写签名,例如 // te
我有一个模板方法,如果存在则执行具有固定参数值的对象的基类方法,如果不存在则不执行,并且它有效。我将 sfinae 与 std::enable_if_t 一起使用来解决方法是否存在。 我不明白的是 -
我一直在阅读 STL 文件,以学习格式化代码的更好方法,并学习提高效率的技巧。我一直在阅读线程文件,但我无法弄清楚某些代码的作用。 template, thread>::value>> exp
我正在尝试基于一个完整的类模板参数启用不同的成员函数,如下所示: #include template struct Foo { template = 0> int bar(int i
假设我们有一些 SFINAE 成员函数: class foo{ template ::value, S> void bar(S&& s); template ::value,
目前,我对这行代码有疑问。它显示此错误消息 error: ‘std::enable_if_t’ has not been declared .我已经包含了 #include 头文件,但它仍然是一样的。
我想弄清楚为什么 SFINAE 不能使用直接模板参数初始化? 当我在此处声明另一个模板参数时,它以这种形式工作: #include template class enable_if {}; tem
更新: 谢谢你,大露营。 这是最后的 struct A . struct A { template> A(Args &&...args) { cout,A>::
考虑以下类: struct S { template std::enable_if_t::value> f() noexcept {} template std
我是一名优秀的程序员,十分优秀!