C++20 成员初始化列表 Clang++ 与 G++

C++20 Member Initialization List Clang++ vs G++

目前使用 Clang++ 13.0.0 和 GCC G++ 11.2.0。

下面的代码已针对上下文进行了简化。当我 运行 使用 g++ 的代码时,它 运行 没有任何警告或错误。当我 运行 使用 Clang 的代码时,出现以下错误:

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

有什么办法可以解决吗?

代码:

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

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

class Test {
    Foo animal;

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

一种可能的方法:

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