考虑以下类:
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/