Why do designated initializers work in C ++?

After a short search on the Internet, I came to the conclusion that the designated initializers are not part of any C ++ standard, but when compiling this code using g ++ (4.7.0)

#include <iostream> using namespace std; int main(int argc, char** argv) { int test[2][2] ={ [0]={1,2}, [1]={3,4}, }; for (int x = 0; x<2;x++) { for (int y = 0; y<2; y++) { cout << test[x][y] << endl; } } return 0; } 

it will compile and work fine.

Am I missing something? From all that I read, C ++ should not support this type of code.

+7
c ++
source share
2 answers

Each compiler usually has its own language extensions. It is valid for both g ++ and, for example, MS VC ++. For example, in MS VC ++ you can use the instruction for each.

0
source share

It seems you have found the gcc compiler function: an undocumented extension that cannot be disabled or prevented using any options (e.g. -pedantic -std=XXXX ).

If you want to be sure that your code complies with the standard, I recommend that you always use a lot of compilers and make sure that your code passes all of them without warning (and use the most stringent warning parameters). gcc and clang are free, so you can always use at least two compilers (and clang is pretty good with standard compliance).

-one
source share

All Articles