【发布时间】:2015-06-11 13:12:00
【问题描述】:
代码:
struct T { T() {} };
struct S
{
T t;
S() noexcept = default;
};
int main()
{
// S s;
}
g++ 4.9.2 接受这一点,没有错误或警告,但是 clang 3.6 和 3.7 报告第 7 行:
error: exception specification of explicitly defaulted default constructor does not match the calculated one
但是,如果S s; 行没有被注释掉,g++ 4.9.2 现在报告:
noex.cc: In function 'int main()':
noex.cc:12:7: error: use of deleted function 'S::S()'
S s;
^
noex.cc:7:5: note: 'S::S() noexcept' is implicitly deleted because its exception-specification does not match the implicit exception-specification ''
S() noexcept = default;
^
哪个编译器适合原始代码?
背景:
g++ 甚至允许将以下内容添加到main:
std::cout << std::is_constructible<S>::value << '\n';
输出0。我在使用 clang 编译一些大量使用模板、SFINAE 和 noexcept 的复杂代码时遇到了这个问题。在该代码中,S 和 T 是模板类;因此行为取决于实例化 S 的类型。对于某些类型,Clang 会因为此错误而拒绝它,而 g++ 允许它,并且 SFINAE 基于 is_constructible 和类似特征工作。
【问题讨论】:
-
因为在 S 构造函数中,您会调用 T 构造函数,这可能会引发任何异常。我相信 Clang 是正确的
-
@SeverinPappadeux 关于异常是正确的,但问题似乎是代码是否应该立即被拒绝,或者
= default的效果是否应该定义为已删除 g++ 似乎在做什么。
标签: c++ c++11 gcc clang noexcept