简体   繁体   English

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

[英]C++20 Member Initialization List Clang++ vs G++

Currently using Clang++ 13.0.0 and GCC G++ 11.2.0.当前使用 Clang++ 13.0.0 和 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.当我使用 g++ 运行代码时,它运行时没有任何警告或错误。 When I run the code using Clang, I get the following error:当我使用 Clang 运行代码时,出现以下错误:

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 
          }
      }
    {}
};

One possible approach:一种可能的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM