gpt4 book ai didi

c++ - 我可以忽略 gcc 警告 : ‘Foo::m_bar’ should be initialized in the member initialization list [-Weffc++]

转载 作者:IT老高 更新时间:2023-10-28 21:55:30 24 4
gpt4 key购买 nike

struct Bar
{
Bar() {}
};


struct Foo
{
Foo() = default;
Bar m_bar;
};

int main()
{
Foo foo;
}

当使用 C++11 default 关键字和 gcc 警告 -Weffc++ 时,gcc 输出:

warning: ‘Foo::m_bar’ should be initialized in the member initialization list [-Weffc++]

忽略此警告是否安全?我应该向 gcc 提交错误吗?

最佳答案

您可以忽略或抑制警告。这是对有效 C++ 指南之一的误解。该指南说更喜欢初始化而不是赋值,但在您的示例中, m_bar 将被初始化。你的代码是正确的。

来源:Jonathan Wakely在 GCC 的错误跟踪器中:

# Item 12: Prefer initialization to assignment in constructors.

Replaced by Item 4: "Make sure that objects are initialized before they're used", and G++ misinterprets the original item anyway and warns about any member without a mem-initializer, which is very annoying: there's no point initializing a std::string, it has a perfectly safe default constructor. My -Wmeminit patch for PR 2972 should replace the current warning for this item, as it only warns about members left uninitialized by the constructor.

(由于这是一个已知问题,因此无需再次将其报告为错误。)

关于c++ - 我可以忽略 gcc 警告 : ‘Foo::m_bar’ should be initialized in the member initialization list [-Weffc++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14002454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com