In-class member initialization with an initializer list using uniform initialization syntax?

257 views Asked by At

I am trying to compile the following with MSVC2013:

class SomeClass
{
    struct SomeStruct { bool a,b,c; };
    SomeStruct ss{false, false, false};
}

The compiler gives me the following error: SomeClass::SomeStruct::SomeStruct: no overloaded function takes 3 arguments.

If I change the code to this:

class SomeClass
{
    struct SomeStruct { bool a,b,c; };
    SomeStruct ss{{false, false, false}};
}

the program compiles and runs fine. Is this a problem with the compiler, or do I not understand the syntax? From what I've been reading, the first version should compile.

2

There are 2 answers

0
pmr On BEST ANSWER

Here is the responsible grammar from N3797:

// after a member declaration:
braced-or-equal-initializer-list:
  = initializer-clause
  braced-init-list

braced-init-list:
  { initializer-list ,OPT }
  { }

initializer-list:
  initializer-clause
  initializer-list, initializer-clause

initializer-clause:
  assignment-expression
  braced-init-list

So I'd say the first statement is correct and it is indeed accepted by a recent gcc and clang.

1
Vlad from Moscow On

If to place semicolons as it is required

class SomeClass
{
    struct SomeStruct { bool a,b,c; };
    SomeStruct ss{false, false, false};
};

then it seems that it is a bug of the MS VC++ 2013 compiler. At least the code is compiled successfully at www.ideone.com.

Structire SomeStruct is an aggregate and should be initialized using an initializer list.