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

            Bug ID: 88865
           Summary: [[no_unique_address]] leads to sizeof(T) == 0, which
                    cannot be
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/rJT5X9

struct B {};
struct A {
    [[no_unique_address]] B a;
    [[no_unique_address]] B b;
    [[no_unique_address]] B c;
    [[no_unique_address]] B d;
};

int f() {
    return sizeof(A);
}

f():
        push    rbp
        mov     rbp, rsp
        mov     eax, 0
        pop     rbp
        ret

In addition to the major issue that sizeof(A) must not be 0, it also must not
be 1 either. It must be (at least) 4.
http://eel.is/c++draft/intro.object#9.sentence-2 is very clear that
[[no_unique_address]] (which clauses 7 and 8 define to mean a "subobject of
zero size") only allows members of *different types* to overlap. a,b,c, and d
are all distinct objects of the same type B, and therefore must have distinct
addresses.

> Two objects with overlapping lifetimes that are not bit-fields may have the 
> same address if one is nested within the other, or if at least one is a 
> subobject of zero size and they are of different types; otherwise, they have 
> distinct addresses and occupy disjoint bytes of storage.


https://godbolt.org/z/160XGN shows that some parts of gcc seem to understand
this rule, some something very strange must be going on.

Reply via email to