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?
[[no_unique_address]]
is to be able to writeB
instead ofA
, and as you showed, it's not likeB
has some padding somewhere to allow for non-unique representations. Filed 97285. – Totter