调用具有通用基类的并集成员的虚拟函数作为另一个成员是未定义行为吗?

时间:2019-06-10 19:54:01

标签: c++ unions type-punning

特别是,我希望能够在嵌入式上下文中使用多态而不分配堆(因此无需动态分配)。我在这里担心的是,在联合中basex处于“活动”状态时访问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

0 个答案:

没有答案