Consider the following code:
#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();
}
Here test()
returns 10 because all A is a union-like struct. My question is, assuming A satisfies StandardLayoutType concept, is casting this
inside B::c
to get pointer to a A::a
undefined behavior?