代码:

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。在该代码中,ST是模板类;因此行为取决于实例化S的类型。 Clang对某些类型的错误拒绝了它,而g++允许它并且SFINAE基于is_constructible和类似的特征工作。

最佳答案

取决于您正在咨询的标准版本。

N3337 [dcl.fct.def.default]/p2:



这会使您的原始代码格式错误。

这被CWG issue 1778更改为读取(N4296 [dcl.fct.def.default]/p3):



这意味着构造函数现在仅定义为删除。 (以上措词合并了N4285所做的更改,ojit_a是C++ 14之后的论文,进行了一些纯净的编辑更改,其目的基本上是编辑性的。N3936版本实质上是相同的。)

大概GCC实现了CWG1778的分辨率,而Clang没有实现。

关于c++ - gcc接受 “noexcept”构造函数的程序,被clang拒绝,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29483120/

10-09 01:52
查看更多