5

我玩弄了 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>);

只有最后一个断言在 GCC (trunk) 和 Clang (trunk) 中都失败了。据我所知,这里没有理由AB表现不同。

这是编译器错误还是有这种差异的原因?

4

0 回答 0