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

前端 未结 2 619
清酒与你
清酒与你 2021-01-01 12:34
struct Bar
{
    Bar() {}
};


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

int main()
{
    Foo foo;
}

When using C++11 default

2条回答
  •  自闭症患者
    2021-01-01 13:02

    Is it alright to ignore this warning? Yes.

    Is it a good idea to ignore this warning? Depends(*)

    Should you file a bug to gcc? No(*)

    (*)

    • default constructor in fact initialises m_bar just fine, you can test that
    • it a bit weird that g++ doesn't get that
    • you selected very verbose warning setting
    • warning is not about correctness of your code, rather about style
    • you can't correct this and keep default constructor for Foo and custom constructor for Bar

    man g++, section -Weffc++

    Warn about violations of the following style guidelines from Scott Meyers’ Effective C++ book:

    • Item 11: Define a copy constructor and an assignment operator for classes with dynamically allocated memory.
    • Item 12: Prefer initialization to assignment in constructors.
    • Item 14: Make destructors virtual in base classes.
    • Item 15: Have "operator=" return a reference to *this.
    • Item 23: Don’t try to return a reference when you must return an object.

    Also warn about violations of the following style guidelines from Scott Meyers’ More Effective C++ book:

    • Item 6: Distinguish between prefix and postfix forms of increment and decrement operators.
    • Item 7: Never overload "&&", "││", or ",".

    When selecting this option, be aware that the standard library headers do not obey all of these guidelines.

提交回复
热议问题