![](/img/trans.png)
[英]g++ and clang++ different behaviour with recursive initialization of a static member
[英]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()) {}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.