What's the struct's initial sequence?

半城伤御伤魂 提交于 2019-12-10 17:12:10

问题


I came across the initial sequence concept. Serching through the Standard for initial sequence phrase gives only 3 results and they don't give a definition.

Section N3797::9.5/1 [class.union]:

If a standard-layout union contains several standard-layout structs that share a common initial sequence (9.2), and if an object of this standard-layout union type contains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of standard-layout struct members;

I wish to look at an example which is demostrated that quote.


回答1:


I believe it's talking about this kind of thing:

union U {
    struct S {
        int a;
        int b;
        int c;
    }
    struct T {
        int x;
        int y;
        float f;
    }
};

It's saying that it's OK to access either U.S.a or U.T.x and that they will be equivalent. Ditto for U.S.b and U.T.y of course. But accessing U.T.f after setting U.S.c would be undefined behaviour.



来源:https://stackoverflow.com/questions/26560311/whats-the-structs-initial-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!