`reinterpret_cast` 在 `this` 内部类似联合的 class 是未定义的行为吗?

Is `reinterpret_cast` on `this` inside union-like class an undefined behavior?

考虑以下代码:

#include <cstdint>

struct B {
    uint32_t c() {
        uint32_t * value = reinterpret_cast<uint32_t *>(this);
        return * value;
    }
};

struct A {
    union {
        B b1;
        B b2;

        uint32_t a { 10 };
    };
};

int test() {
    A a;
    return a.b1.c();
}

这里 test() returns 10 因为所有的 A 都是一个类联合结构。我的问题是,假设 A 满足 StandardLayoutType 概念,是否将 this 投射到 B::c 中以获取指向 A::a 未定义行为的指针?

这是未定义的行为。总的来说,联合包含 uint32_tB

  • 如果它是 B,则强制转换是非法的(因为它不是 uint32_t,所以不能向它强制转换)。
  • 如果它是 uint32_t,则调用 .c() 成员是非法的,因为您无法访问 b1 成员(不是活跃的联盟成员)。

在这种情况下(感谢 @StoryTeller's 评论)活跃的联合成员是 auint32_t),因为它是唯一具有默认初始化的成员,因此调用 a.b1.c() 是 UB。