11

Having a structure like this in C++11:

struct von
{
    std::string Name;
    unsigned int ID;
    std::vector<std::string> Checks;
};

Should it be initialized like this:

    von v = {"",0,{}};

Or like this:

    von v = {};

Both ways seem to work, but the compiler warns about -Wmissing-field-initializers on the latter example.

Edit:
Here are my compiler options:

g++ main.cpp -ansi -Wall -Wextra -Weffc++ -std=c++0x

I'm using g++ (Debian 4.6.2-12) 4.6.2

leiyc
  • 903
  • 11
  • 23
01100110
  • 2,294
  • 3
  • 23
  • 32
  • 2
    There are no initializer lists in your example, only list initialization. The best way might be `von v{};`. – Kerrek SB Mar 04 '12 at 18:07
  • 1
    von v{}; also complains of missing initializers for the members. It's a -Wmissing-field-initializer warning. – 01100110 Mar 04 '12 at 18:21
  • Hm, you're right. It's a shame you can't value-initialize an automatic variable... `von v{{},0,{}};` is the next-best thing. – Kerrek SB Mar 04 '12 at 18:46
  • Please make your title describe the question. – Lightness Races in Orbit Mar 04 '12 at 19:02
  • 1
    In the case of zero arguments, I personally think the warning from `-Wmissing-field-initializer` is overly pedantic. – StilesCrisis Mar 04 '12 at 19:11
  • @StilesCrisis Agreed, but not only then, also in the case of a single value of 0. –  Mar 04 '12 at 19:13
  • I have g++ 4.6.2, with no support for C11. Your code compiles without a warning with `g++ -Wall -pedantic --std=c++0x`. What compiler and command line options are you using? – j4x Mar 04 '12 at 19:26
  • 1
    Warning for the empty initializer list case is a false positive [that has been fixed](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61489). – vilpan Jun 24 '17 at 13:43

1 Answers1

3

This doesn't require initializer_list at all and work perfectly fine with C++03. Edit: (Ok, for the initialization of the vector you need C++11) In a struct or array initialization, all not explicitly given values are zero-initialized, so if that's what you want = {}; will work just fine.

cooky451
  • 3,460
  • 1
  • 21
  • 39