[ACCEPTED]-Why are designated initializers not implemented in g++-designated-initializer

Accepted answer
Score: 13

I ran into this same problem today. g++ with 7 -std=c++11 and c++14 does support designated 6 initializers, but you can still get a compilation 5 error "test.cxx:78:9: sorry, unimplemented: non-trivial 4 designated initializers not supported" if 3 you don't initialize the struct in the order 2 in which it's members have been defined. As 1 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};
Score: 11

As I noted in a comment, G++ doesn't support 14 C99 standard designated initialisers, but 13 it does support the GNU extension to C90 12 which allows designated initialisers. So 11 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 10 to be a bad interaction of co-ordination 9 between the C and C++ standards committees 8 (there is no particularly good reason why 7 C++ doesn't support the C99 syntax, they 6 just haven't considered it) and GCC politics 5 (C++ shouldn't support C99 syntax just because 4 it's in C99, but it should support GNU extension 3 syntax that achieves exactly the same thing 2 because that's a GNU extension that can 1 be applied to either language).

Score: 9

C++ does not support this. It will not even 3 be in the C++0x standards it seems: http://groups.google.com/group/comp.std.c++/browse_thread/thread/8b7331b0879045ad?pli=1

Also, why 2 are you trying to compile the Linux kernel 1 with G++?

Score: 4

It is officially supported in C++20, and is already implemented in g++8.2 (even without the std=c++2a flag).

0

Score: 2

As of at least g++-4.8 this is now supported 1 by default.

Score: 0

What about anonymous unions?

In C I can 8 have this:

struct vardir_entry {
    const uint16_t id;
    const uint8_t sub;
    const char *name;
    const uint8_t type;

    const union {   
        struct vardir_lookup lookup;
        struct vardir_min_max_conf minmax;       
    };

    const union {
        const struct vardir_value_target_const const_value;
        const struct vardir_value_target value;
    };
};

And initialized like this:

static const struct vardir_entry _directory[]{
        { .id = 0xefef, .sub = 0, .name = "test", .type = VAR_UINT32, .minmax = { .min = 0, .max = 1000 }, .value = VARDIR_ENTRY_VALUE(struct obj, &obj, member) }
    };

However 7 under g++ even with c++14 this gives the 6 same "sorry, unimplemented" error. We do 5 need to be able to define C variables in 4 C++ when we at least want to unit test C 3 code with C++ test framework. The fact that 2 such a valuable feature from C is not being 1 supported is quite a shame.

More Related questions