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

            Bug ID: 97038
           Summary: [[no_unique_address]] support anonymous unions
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: no_unique_address at mail dot de
  Target Milestone: ---

Compiling the following source file with g++ -std=c++20 -Wall -pedantic
--
struct A {};
struct B {};
struct C
{
    [[no_unique_address]] union
    {
        [[no_unique_address]] A a;
        [[no_unique_address]] B b;
    } /*u*/;
    char c;
};

static_assert(sizeof(C) == sizeof(char));
--
leads to the following output:

<source>:6:5: warning: attribute ignored in declaration of 'union C::<unnamed>'
[-Wattributes]

    6 |     {

      |     ^

<source>:6:5: note: attribute for 'union C::<unnamed>' must follow the 'union'
keyword

<source>:13:25: error: static assertion failed

   13 | static_assert(sizeof(C) == sizeof(char));

      |               ~~~~~~~~~~^~~~~~~~~~~~~~~

Compiler returned: 1

See also: https://godbolt.org/z/GcW9cr

This works fine if the union is not anonymous.
This also works on clang 10.

It would be nice if it could be supported on gcc.

Reply via email to