22
struct Bar
{
    Bar() {}
};


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

int main()
{
    Foo foo;
}

When using C++11 default keyword and gcc warning -Weffc++, gcc outputs:

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

Is it safe to ignore this warning? Should I file a bug to gcc?

aculich
  • 13,401
  • 8
  • 58
  • 69
qdii
  • 11,387
  • 7
  • 54
  • 107
  • 3
    "Should I file a bug to gcc" - no. When you think it's a compiler bug, then it isn't. –  Dec 22 '12 at 11:31
  • 2
    I don't see the warnings if I do `Bar() = default`. – Pubby Dec 22 '12 at 11:43
  • 5
    Pity there's no downvoting comments... – StoryTeller - Unslander Monica Dec 22 '12 at 11:57
  • 1
    Unfortunately, `-Weffc++` is not really useful currently. It is one of the only warnings that didn't make it onto my huge list of warnings that I use on every project: http://stackoverflow.com/questions/5088460/flags-to-enable-thorough-and-verbose-g-warnings/9862800#9862800 – David Stone Dec 22 '12 at 16:06

2 Answers2

29

You can ignore or suppress the warning. This is a misinterpretation of one of the Effective C++ guidelines. The guideline says to prefer initialisation to assignment, but in your example, m_bar will be initialised. Your code is correct.

Source: Jonathan Wakely in GCC's bug tracker:

# 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.

(And as it's a known issue, there's no need to report it as a bug again.)

6

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.

user
  • 6,669
  • 7
  • 39
  • 77
Dima Tisnek
  • 9,367
  • 4
  • 48
  • 106