Gab*_*cze 6 c++ type-traits object-identity c++20
我尝试了 C++20 的[[no_unique_address]]属性,并在将其与类型特征一起使用时发现了一些有趣的行为has_unique_object_representations:
#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>);
Run Code Online (Sandbox Code Playgroud)
只有最后一个断言对于 GCC(主干)和 Clang(主干)都失败。据我所知,这里没有理由A采取B不同的行为。
这是编译器错误还是有造成这种差异的原因?