相关疑难解决方法(0)

将联合重新解释为不同的联合

我有一个标准布局联合,其中包含一大堆类型:

union Big {
    Hdr h;

    A a;
    B b;
    C c;
    D d;
    E e;
    F f;
};
Run Code Online (Sandbox Code Playgroud)

每种类型的A直通F是标准布局和具有作为其第一元件类型的对象Hdr.该Hdr标识了工会的积极成员为,所以这是变体样.现在,我处于一种我确定知道的情况(因为我检查过),活跃成员是a B或a C.实际上,我已将空间减少到:

union Little {
    Hdr h;

    B b;
    C c;
};
Run Code Online (Sandbox Code Playgroud)

现在,是以下明确定义还是未定义的行为?

void given_big(Big const& big) {
    switch(big.h.type) {
    case B::type: // fallthrough
    case C::type:
        given_b_or_c(reinterpret_cast<Little const&>(big));
        break;
    // ... other cases here ...
    }
}

void given_b_or_c(Little const& little) {
    if (little.h.type == B::type) {
        use_a_b(little.b);
    } else …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++11

35
推荐指数
2
解决办法
1213
查看次数

标签 统计

c++ ×1

c++11 ×1

language-lawyer ×1