Home > database >  C 20 Member Initialization List Clang vs G
C 20 Member Initialization List Clang vs G

Time:11-27

Currently using Clang 13.0.0 and GCC G 11.2.0.

The code below has been simplified for context. When I run the code using g , it runs without any warnings or errors. When I run the code using Clang, I get the following error:

field 'cat' is uninitialized when used here [-Werror,-Wuninitialized]

Is there any way to resolve this?

Code:

struct Bar {
    Object *ptr;
    int y;
};

struct Foo {
    Object *ptr;
    Bar cat;
};

class Test {
    Foo animal;

    Test()
    : animal{
          generateObject(),
          {
             animal.ptr,
             0 
          }
      }
    {}
};

CodePudding user response:

One possible approach:

class Test {
private:
  explicit Test(Object* ptr)
  : animal{ptr, {ptr, 0}} {}
public:
  Test() : Test(generateObject()) {}
};
  • Related