Is it legal and well defined behavior to use a union for conversion between two structs with a common initial sequence (see example)?

后端 未结 2 632
一向
一向 2021-02-05 10:31

I have an API with a publicly facing struct A and an internal struct B and need to be able to convert a struct B into a struct A. Is the following code legal and well de

2条回答
  •  借酒劲吻你
    2021-02-05 11:11

    It is legal in both C and C++

    For example, in C99 (6.5.2.3/5) and C11 (6.5.2.3/6):

    One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the complete type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

    Similar provisions exists in C++11 and C++14 (different wording, same meaning).

提交回复
热议问题