6

I've playing around with C++20's [[no_unique_address]] attribute and found some interesting behavior when using it with the has_unique_object_representations type trait:

#include <type_traits>

struct Empty {};

struct A : public Empty {
    int x;
};

struct B {
    [[no_unique_address]] Empty e;
    int x;
};

static_assert (sizeof(A) == sizeof(int));
static_assert (sizeof(B) == sizeof(int));

static_assert(std::has_unique_object_representations_v<A>);
static_assert(std::has_unique_object_representations_v<B>);

Only the last assertion fails with both GCC (trunk) and Clang (trunk). As far as I can tell, there's no reason for A and B to behave differently here.

Is this a compiler bug or is there a reason for this difference?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 2
    Seems like a bug. The whole point of `[[no_unique_address]]` is to be able to write `B` instead of `A`, and as you showed, it's not like `B` has some padding somewhere to allow for non-unique representations. Filed [97285](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97285). – Barry Oct 04 '20 at 14:38
  • 1
    Added a bug also for clang [47722](https://bugs.llvm.org/show_bug.cgi?id=47722). – Amir Kirsh Oct 04 '20 at 16:21
  • Thanks for the answers and for submitting the bug reports! @Barry could you also submit this as an answer so I can accept it? Thanks! – Gabor Bencze Oct 04 '20 at 18:40

0 Answers0