I need some help understanding some subtleties of C++0x consolidated initializer lists.
Why does...
#include <iostream>
int main()
{
struct Foo
{
public:
struct Bar
{
public:
Bar(int a, int b, int c){}
int ma, mb, mc;
} mBar[2];
Foo(int a, int b, int c)
: mBar{{a, b, c},
{a+10, b+10, c+10}}
{}
} mFoo(1, 2, 3);
return 0;
}
...result in this compiler error...
>g++ -std=c++0x main.cpp
main.cpp: In constructor ‘main()::Foo::Foo(int, int, int)’:
main.cpp:18: error: bad array initializer
>
...whereas this...
#include <iostream>
int main()
{
struct Foo
{
public:
struct Bar
{
public:
////////////////////Bar(int a, int b, int c){}
int ma, mb, mc;
} mBar[2];
Foo(int a, int b, int c)
: mBar{{a, b, c},
{a+10, b+10, c+10}}
{}
} mFoo(1, 2, 3);
return 0;
}
...does not...
>g++ -std=c++0x main.cpp
>
...whereas if I compile the first version of the above code with a different compiler, it compiles...
>/opt/mv_7/arm/tools/arm-gnu/bin/arm-montavista-linux-gnueabi-g++ -std=c++0x main.cpp
>
?
I'd be very grateful for clarity on this. I thought I'd come to understand initializer lists because the first stab at this topic was with the cross-compiler (that compiled the first version of code with no complaints), but the above discrepancy has me puzzled now. Thank you.
In the comments you indicated that you were using gcc 4.4.7.
This is a relatively old compiler. I had no issues compiling the given code using gcc 5.3.1 in -std=c++14 mode.
If this code does not compile with the -std=c++11 flag with your compiler, it's because this older compiler hasn't yet fully implemented the C++1x standard.