简体   繁体   English

C11 中的匿名结构和联合是否被错误描述?

[英]Have anonymous structs and unions in C11 been incorrectly described?

Sayeth the C standard, regarding anonymous structs and unions: Sayeth C 标准,关于匿名结构和联合:

6.7.2.1 p13. 6.7.2.1 第 13 页。 An unnamed member whose type specifier is a structure specifier with no tag is called an anonymous structure;类型说明符是没有标记的结构说明符的未命名成员称为匿名结构; an unnamed member whose type specifier is a union specifier with no tag is called an anonymous union.类型说明符是没有标记的联合说明符的未命名成员称为匿名联合。 The members of an anonymous structure or union are considered to be members of the containing structure or union.匿名结构或联合的成员被认为是包含结构或联合的成员。 This applies recursively if the containing structure or union is also anonymous.如果包含结构或联合也是匿名的,这将递归地适用。

Note emphasis: rather than the members of the anonymous struct/union being in the scope of the containing struct/union, they are fully members of it.注意强调:而不是匿名结构/联合的成员在包含结构/联合的范围内,它们是它的完全成员 But there are responsibilities attached to that:但这有责任:

6.7.2.1 p16. 6.7.2.1 第 16 页。 The size of a union is sufficient to contain the largest of its members.联合的大小足以包含其最大的成员。 The value of at most one of the members can be stored in a union object at any time.最多一个成员的值可以随时存储在联合对象中。 A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.一个指向联合对象的指针,经过适当的转换,指向它的每个成员(或者如果一个成员是一个位域,则指向它所在的单元),反之亦然。

Taken together, those seem to imply that the members of an anonymous struct within a (named) union behave like co-located, mutually-exclusive members of the union.综上所述,这些似乎意味着(命名)联合中的匿名结构的成员表现得像联合中位于同一位置、互斥的成员。 So the following should work:所以以下应该工作:

union Foo
{
    struct
    {
        char a;
        char b;
    };
};

int main(void) {
    union Foo f;
    assert(&f == &(f.a) && &f == &(f.b));
}

Of course, it doesn't, and we wouldn't want it to... there'd be no reason for anonymous structs/unions if they worked like the above.当然,它没有,我们也不希望它......如果匿名结构/联合像上面那样工作,就没有理由使用匿名结构/联合。 Still, the Standard seems unambiguous on that point.尽管如此,标准在这一点上似乎毫不含糊。 Is there something I'm missing?有什么我想念的吗?

TL;DR TL; 博士

This looks like a wording issue, they should not overlap.这看起来像是一个措辞问题,它们不应该重叠。

Details详情

This is covered in Defect Report (DR) 499 which asks: 缺陷报告 (DR) 499 中涵盖了这一点,其中要求:

Given the following code:鉴于以下代码:

 union U { struct { char B1; char B2; char B3; char B4; }; int word; } u;

Does the storage of B1, B2, B3 and B4 overlap? B1、B2、B3 和 B4 的存储是否重叠?

According to 6.7.2.1#13, the members should overlap in storage as they become members of 'union U'.根据 6.7.2.1#13,成员在成为“联合 U”的成员时应该在存储中重叠。
At least one implementation (GCC) seems to NOT consider them to be overlapping.至少一个实现(GCC)似乎不认为它们是重叠的。
At least one implementation (IBM's XL LE AIX) considers them to be overlapping as the standard currently states.至少有一个实现(IBM 的 XL LE AIX)认为它们与标准当前声明的一样重叠。

And the committees response was:委员会的回应是:

The storage does not overlap.存储不重叠。

A related issue is to be found in DR 502 and both may be resolved with coordinated wording changes.DR 502 中可以找到一个相关的问题,两者都可以通过协调的措辞更改来解决。

and (emphasis added)和(强调)

Change §6.7.2.1 p13 from:将 §6.7.2.1 p13 更改为:

An unnamed member of structure type with no tag is called an anonymous structure;没有标记的结构类型的未命名成员称为匿名结构; an unnamed member of union type with no tag is called an anonymous union.没有标记的联合类型的未命名成员称为匿名联合。 The members of an anonymous structure or union are considered to be members of the containing structure or union.匿名结构或联合的成员认为是包含结构或联合的成员 This applies recursively if the containing structure or union is also anonymous.如果包含结构或联合也是匿名的,这将递归地适用。

to:到:

An unnamed member of structure type with no tag is called an anonymous structure;没有标记的结构类型的未命名成员称为匿名结构; an unnamed member of union type with no tag is called an anonymous union.没有标记的联合类型的未命名成员称为匿名联合。 The names of members of an anonymous structure or union are added to the name space of the containing structure or union.匿名结构或联合成员的名称被添加到包含结构或联合的名称空间中 This applies recursively if the containing structure or union is also anonymous.如果包含结构或联合也是匿名的,这将递归地适用。

would adequately resolve the issue.将充分解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM