Inconsistency in has_unique_object_representations and empty subobjects
Asked Answered
D

0

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?

Dunnage answered 4/10, 2020 at 14:13 Comment(3)
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.Totter
Added a bug also for clang 47722.Plasmagel
Thanks for the answers and for submitting the bug reports! @Totter could you also submit this as an answer so I can accept it? Thanks!Dunnage

© 2022 - 2024 — McMap. All rights reserved.