Is there any specific reason why has support for designated initializers not been added to g++? Is the reason that C99 standards came late and g++ was developed earlier and later people didn't care about this issue, or there is some inherent difficulty in implementing designated initializers in the grammar of C++?
With C++20, we get a handy way of initializing data members. The new feature is called designated initializers and might be familiar to C programmers.
Designated initializers, a C99 feature, are supported for aggregate types, including arrays, structures, and unions. A designated initializer, or designator, points out a particular element to be initialized. A designator list is a comma-separated list of one or more designators.
In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression or a comma-separated list of expressions placed in curly brackets (braces).
I ran into this same problem today. g++ with -std=c++11 and c++14 does support designated initializers, but you can still get a compilation error "test.cxx:78:9: sorry, unimplemented: non-trivial designated initializers not supported" if you don't initialize the struct in the order in which it's members have been defined. As an example
struct x
{
int a;
int b;
};
// This is correct
struct x x_1 = {.a = 1, .b = 2};
// This will fail to compile with error non-trivial designated initializer
struct x x_2 = {.b = 1, .a = 2};
As I noted in a comment, G++ doesn't support C99 standard designated initialisers, but it does support the GNU extension to C90 which allows designated initialisers. So this doesn't work:
union value_t {
char * v_cp;
float v_f;
};
union value_t my_val = { .v_f = 3.5f };
But this does:
union value_t my_val = { v_f: 3.5f };
This seems to be a bad interaction of co-ordination between the C and C++ standards committees (there is no particularly good reason why C++ doesn't support the C99 syntax, they just haven't considered it) and GCC politics (C++ shouldn't support C99 syntax just because it's in C99, but it should support GNU extension syntax that achieves exactly the same thing because that's a GNU extension that can be applied to either language).
C++ does not support this. It will not even be in the C++0x standards it seems: http://groups.google.com/group/comp.std.c++/browse_thread/thread/8b7331b0879045ad?pli=1
Also, why are you trying to compile the Linux kernel with G++?
It is officially supported in C++20, and is already implemented in g++8.2 (even without the std=c++2a
flag).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With