C++ gcc:缺少关于构造函数中初始化顺序的警告

C++ gcc:缺少关于构造函数中初始化顺序的警告,c++,gcc,initialization,initializer-list,C++,Gcc,Initialization,Initializer List,gcc是否应该警告C类中成员变量a和b的初始化顺序?基本上,对象b是初始化的,它的构造函数在对象A之前调用。这意味着b使用未初始化的A #include <iostream> using namespace std; class A { private: int x; public: A() : x(10) { cout << __func__ << endl; } friend class

gcc是否应该警告C类中成员变量
a
b
的初始化顺序?基本上,对象b是初始化的,它的构造函数在对象A之前调用。这意味着
b
使用未初始化的
A

#include <iostream>

using namespace std;

class A
{
    private:
        int x;
    public:
        A() : x(10) { cout << __func__ << endl; }
        friend class B;
};

class B
{
    public:
        B(const A& a) { cout << "B: a.x = " << a.x << endl; }
};

class C
{
    private:
        //Note that because b is declared before a it is initialized before a
        //which means b's constructor is executed before a.
        B b;
        A a;

    public:
        C() : b(a) { cout << __func__ << endl; }
};

int main(int argc, char* argv[])
{
    C c;
}

为了使其成为初始化问题的顺序,您需要实际尝试以错误的顺序初始化子对象:

public:
    C() : a(), b(a) { cout << __func__ << endl; } 
          ^^^ this is attempted initialization out of order
公共:

C():a(),b(a){你能告诉它警告你吗?标志是-Wreorder,它是用-Wall打开的。你应该确保编译时使用
-Wall-Werror-Wextra-pedantic errors
。代码不会编译,因为gcc会警告你正在使用uninitialized@bamboon:你确定吗?这里的代码编译得很好。它看起来像你的初始化顺序很好。只是你没有初始化你的一个成员然后使用它。我没有从gcc或clang得到最高警告级别的警告,这有点奇怪。你应该向各自的跟踪器提交bug。我认为你是正确的。正如你提到的,从技术上讲,它与initia的顺序没有任何关系化。
public:
    C() : a(), b(a) { cout << __func__ << endl; } 
          ^^^ this is attempted initialization out of order