c++ - 构造函数在 g++ 和 clang++ 中委托(delegate)给自己

标签 c++ c++11 constructor language-lawyer delegating-constructor

考虑以下程序。我不小心弄错了。

struct T {
    int s;
    T() : T() {
        s=9;
    }
};
int main() {
    T t;
}

以上代码在某些版本的 g++ 中编译和运行良好,如 g++ 4.8.1(参见现场演示 here)和 clang++ 3.6.0(参见现场演示 here)和 MSVC++ 2015,但在运行时崩溃。它给我 segmentation fault 错误。我认为这是由于递归,我的意思是递归调用构造函数。但最新版本的 g++ 和 clang++ 无法通过给出以下错误来编译此代码:

g++ 4.9.2 给出以下错误(参见现场演示 here)

prog.cc: In constructor 'T::T()':
prog.cc:3:10: error: constructor delegates to itself
  T() : T() {

clang++ 给出以下错误(参见现场演示 here)

main.cpp:4:8: error: constructor for 'T' creates a delegation cycle [-Wdelegating-ctor-cycles]
        T() : T() {
              ^
1 error generated.

那么,这里的问题是,根据标准,哪个编译器是正确的?这些编译器之一有错误吗?上面的程序到底发生了什么?如果我的理解有误,请纠正我。为什么同一个程序在这些编译器的不同版本中表现出不同的行为?

最佳答案

从 C++11 开始,[class.base.init]¶6:

If a constructor delegates to itself directly or indirectly, the program is ill-formed; no diagnostic is required.

所有编译器都是正确的——代码被破坏了,编译器不需要告诉你。此时你有UB;来自 [intro.compliance]¶2:

If a program contains a violation of a rule for which no diagnostic is required, this International Standard places no requirement on implementations with respect to that program.

关于c++ - 构造函数在 g++ 和 clang++ 中委托(delegate)给自己,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38486235/

相关文章:

c# - 在c#项目中导入c++ dll

c++ - 为什么 C++ 在将一个对象复制到另一个对象时需要对两个对象进行低级 const 限定?

c++ - 为什么我必须调用 operator<< 作为 SFINAE 使用 void_t 的方法?

python - 如何使用 [] 作为 python 中命名函数参数的默认值?

c++ - boost 许可变更

c++ - 是否有可能获得在 GCC 中编译的函数的名称?

c++ - 如何消除这种与继承相关的代码异味?

c++ - 枚举类不是类或命名空间

java - 明确消除Java中重载构造函数的歧义

C++ Basic Constructor 不打印文本