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

--- Comment #8 from Pedro Alves <pedro at palves dot net> ---
Thank you, I missed the "same type" constraint.  It's also mentioned at
https://en.cppreference.com/w/cpp/language/attributes/no_unique_address .

Indeed, if I use distinct empty types, GCC and Clang lay them at the same
address:

$ cat foo3.cc 
#include <stdio.h>
#include <stddef.h>

struct Empty1 {};
struct Empty2 {};
struct Empty3 {};

struct S
{
  [[no_unique_address]] Empty1 empty1;
  [[no_unique_address]] Empty2 empty2;
  [[no_unique_address]] Empty3 empty3;
} s;

int main ()
{
  printf ("&s.empty1 = %p\n", &s.empty1);
  printf ("&s.empty2 = %p\n", &s.empty2);
  printf ("&s.empty3 = %p\n", &s.empty3);
  printf ("s = %p\n", &s);
  printf ("offset empty1 = %ld\n", offsetof (struct S, empty1));
  printf ("offset empty2 = %ld\n", offsetof (struct S, empty2));
  printf ("offset empty3 = %ld\n", offsetof (struct S, empty3));
  return 0;
}

$ ./foo3 
&s.empty1 = 0x55a5dc7aa031
&s.empty2 = 0x55a5dc7aa031
&s.empty3 = 0x55a5dc7aa031
s = 0x55a5dc7aa031
offset empty1 = 0
offset empty2 = 0
offset empty3 = 0

$ readelf -dw foo3| grep data_member_location
    <b6>   DW_AT_data_member_location: -1
    <c0>   DW_AT_data_member_location: -1
    <ca>   DW_AT_data_member_location: -1

So (-1, -1, -1) in the DWARF and (0, 0, 0) at run time.

Reply via email to