https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126131

            Bug ID: 126131
           Summary: No way to use designated initializers for anonymous
                    union warning/error free with -std=gnu++20 or later?
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sirl at gcc dot gnu.org
  Target Milestone: ---

When compiling this snippet with "g++-16 -std=gnu++20 -Wmissing-braces" there
is always either an error or a warning:

typedef void fctp1(void *);

struct s {
        int i;
        union {
                fctp1 *p_fct1;
                struct s *p_s;
        };
};

struct s sarr1[] = {
        { .i = 0, { .p_s = nullptr } }
};

struct s sarr2[] = {
        { .i = 0, { .p_fct1 = nullptr } }
};

struct s sarr3[] = {
        { .i = 0, .p_fct1 = nullptr }
};


t.cpp:12:19: error: either all initializer clauses should be designated or none
of them should be
   12 |         { .i = 0, { .p_s = nullptr } }
      |                   ^
t.cpp:16:19: error: either all initializer clauses should be designated or none
of them should be
   16 |         { .i = 0, { .p_fct1 = nullptr } }
      |                   ^
t.cpp:21:1: warning: missing braces around initializer for 's::<unnamed union>'
[-Wmissing-braces]
   21 | };
      | ^

With -std=gnu++17 there was only the braces warning and that was easy to fix by
adding the braces. But with -std=gnu++20 or later there is no longer a way to
get this warning/error free except by not using designated initializers at all.

Is this a bug or does gnu++20 really require this?

Reply via email to