Home > Blockchain >  Why is designator initialization compiled in c 11?
Why is designator initialization compiled in c 11?

Time:06-01

If I understood correctly, the article https://en.cppreference.com/w/cpp/language/aggregate_initialization says that designated initialization is allowed starting from c 20, and is not allowed in c 11.

So why is the following compiled in c 11? g -std=c 11 main.cpp

struct A { int x; int y; int z; };

int main(int argc, char const *argv[])
{
    A b{.x = 1, .z = 2};
    return 0;
}

My first guess was that there is some kind of gcc extension that supports it, but clang compile this code as well (clang -std=c 11 main.cpp)

CodePudding user response:

Why is designator initialization compiled in c 11?

The program is ill-formed in C 11. The C language doesn't disallow ill-formed programs from compiling.

My first guess was that there is some kind of gcc extension that supports it, but clang compile this code as well

Your first guess is good, and you can simply expand it to arrive at the answer: The language is extended by both GCC and Clang. Clang often attempts to be as compatible with GCC as possible. For better and for worse.

  • Related