特别是,我希望能够在嵌入式上下文中使用多态而不分配堆(因此无需动态分配)。我在这里担心的是,在联合中base
或x
处于“活动”状态时访问y
成员似乎是类型绑定的一个实例,即使它们共享相同的内容。初始标头(和vtable结构)。下面的代码是否被视为定义的行为?
#include <iostream>
#include <new>
using namespace std;
struct Base {
virtual const char *foo() { return "base"; }
};
struct X: public Base {
const char *foo() override { return "d1"; }
};
struct Y: public Base {
const char *foo() override { return "d2"; }
};
union DerivedAny {
DerivedAny() {}
Base& get() { return *launder(&b); }
Base b = {};
X x;
Y y;
};
DerivedAny objs[3];
int main() {
new (&objs[1].x) X;
new (&objs[2].y) Y;
cout << objs[0].get().foo() << endl;
cout << objs[1].get().foo() << endl;
cout << objs[2].get().foo() << endl;
}
我已经使用GCC和Clang对其进行了编译,在两种情况下它们似乎都返回了预期的输出:
base
d1
d2