Do array elements count as a common initial sequence?

前端 未结 1 1362
时光取名叫无心
时光取名叫无心 2021-01-17 11:41

Sort of related to my previous question:

Do elements of arrays count as a common initial sequence?

struct arr4 { int arr[4]; };
struct arr2 { int arr         


        
相关标签:
1条回答
  • 2021-01-17 12:12

    C++11 says (9.2):

    If a standard-layout union contains two or more standard-layout structs that share a common initial sequence, and if the standard-layout union object currently contains one of these standard-layout structs, it is permitted to inspect the common initial part of any of them. Two standard-layout structs share a common initial sequence if corresponding members have layout-compatible types and either neither member is a bit-field or both are bit-fields with the same width for a sequence of one or more initial members.

    As to whether arrays of different size form a valid common initial sequence, 3.9 says:

    If two types T1 and T2 are the same type, then T1 and T2 are layout-compatible types

    These arrays are not the same type, so this doesn't apply. There is no special further exception for arrays, so the arrays may not be layout-compatible and do not form a common initial sequence.

    In practice, though, I know of a compiler (GCC) which:

    • ignores the "common initial sequence" rule, and
    • allows type punning anyway, but only when accesses are "via the union type" (as in your example), in which case the "common initial sequence" rule is obeyed indirectly (because a "common initial sequence" implies a common initial layout on the architectures the compiler supports).

    I suspect many other compilers take a similar approach. In your example, where you type-pun via the union object, such compilers will give you the expected result - reading from the inactive member should give you value written via the inactive member.

    0 讨论(0)
提交回复
热议问题