编码:
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
和类似的特征工作。