Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top