c++ - C++0x Initializer Lists -
i need understanding 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 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 compile first version of above code different compiler, compiles...
>/opt/mv_7/arm/tools/arm-gnu/bin/arm-montavista-linux-gnueabi-g++ -std=c++0x main.cpp >
?
i'd grateful clarity on this. thought i'd come understand initializer lists because first stab @ topic cross-compiler (that compiled first version of code no complaints), above discrepancy has me puzzled now. thank you.
in comments indicated using gcc 4.4.7.
this relatively old compiler. had no issues compiling given code using gcc 5.3.1 in -std=c++14 mode.
if code not compile -std=c++11 flag compiler, it's because older compiler hasn't yet implemented c++1x standard.
Comments
Post a Comment