这是C11匿名结构吗? [英] Is this a C11 anonymous struct?

查看:126
本文介绍了这是C11匿名结构吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究C11草案,它说

I was looking into the C11 draft and it says

没有标签的结构类型的未命名成员称为匿名结构;没有标签的联合体类型的未命名成员称为匿名联合体.匿名结构或联合的成员被视为包含结构或联合的成员.

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.

所以我构造了以下测试用例

So I constructed the following testcase

// struct type with no tag
typedef struct {
  unsigned char a;
  unsigned char b;
  // ... Some other members ...
  unsigned char w;
} AToW;

union
{
  AToW; // <- unnamed member
  unsigned char bytes[sizeof(AToW)];
} myUnion;

Clang和GCC都抱怨该匿名成员,称声明无效.我是在做错什么,还是他们根本不支持该功能?

Clang and GCC both complain about the unnamed member, saying that the declaration has no effect. Did I do something wrong, or do they simply not support that feature yet?

推荐答案

不,那不是匿名会员.

一个例子是:

struct outer {
    int a;
    struct {
        int b;
        int c;
    };
    int d;
};

包含成员bc的内部结构是struct outer未命名成员.该未命名成员bc的成员被视为包含结构的成员.

The inner structure containing members b and c is an unnamed member of struct outer. The members of this unnamed member, b and c, are considered to be members of the containing structure.

对于包含联合而不是包含结构,这可能更有用.特别是,它可以用来定义类似于Pascal或Ada变体记录的内容:

This is probably more useful with a contained union rather than a contained structure. In particular, it can be used to define something similar to a Pascal or Ada variant record:

enum variant_type { t_int, t_double, t_pointer, t_pair };
struct variant {
    enum variant_type type;
    union {
        int i;
        double d;
        void *p;
        struct {
            int x;
            int y;
        };
    };
};

这使您可以直接将idp引用为struct variant对象的成员,而不是为变体部分创建人为的名称.如果某些变体需要一个以上的成员,则可以在匿名联合中嵌套匿名结构.

This lets you refer to i, d, and p directly as members of a struct variant object rather than creating an artificial name for the variant portion. If some variants require more than one member, you can nest anonymous structures within the anonymous union.

(与Pascal和Ada的区别在于,在给定type成员的值的情况下,没有机制可以强制哪个变体处于活动状态;对于您来说这就是C.)

(It differs from Pascal and Ada in that there's no mechanism to enforce which variant is active given the value of the type member; that's C for you.)

在您的示例中,AToW是先前定义的结构类型的typedef.您不允许裸露

In your example, AToW is a typedef for a struct type that you defined previously. You're not permitted to have a bare

AToW;

在结构定义的中间,除了光秃秃的

in the middle of a struct definition, any more than you can have a bare

int;

C11增加了在另一个结构中定义嵌套的匿名结构的功能,但是只能在那时定义一个新的匿名结构类型.您不能具有先前定义的类型的匿名struct成员.已经定义了语言可以以允许它使用,并且语义(我认为)相当简单-但是定义两种不同的方式来做同一件事没有多大意义. (对于上面的结构",请阅读结构或联合".)

C11 added the ability to define a nested anonymous struct within another struct, but only by defining a new anonymous struct type at that point. You can't have an anonymous struct member of a previously defined type. The language could have been defined to permit it, and the semantics would (I think) be reasonably straightforward -- but there wasn't much point in defining two different ways to do the same thing. (For "struct" in the above, read "struct or union".)

引用 N1570草案(其中非常接近已发布的2011 ISO C标准),第6.7.2.1节第13段:

Quoting the N1570 draft (which is very close to the released 2011 ISO C standard), section 6.7.2.1 paragraph 13:

一个未命名的成员,其类型说明符是具有以下内容的结构说明符 没有标签称为匿名结构;类型不明的成员 说明符是没有标签的联合说明符,称为 anonymous 联合.匿名结构或联合的成员被认为 成为包含结构或联合的成员.这适用 如果包含结构或联合也是匿名的,则递归地进行.

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.

结构说明符由关键字struct组成,后跟一个可选的标识符(在这种情况下,省略标记),后跟由{.在您的情况下,AToW是类型名称,而不是结构说明符,因此它不能用于定义匿名结构.

A structure specifier consists of the keyword struct, followed by an optional identifier (the tag, omitted in this case), followed by a sequence of declarations enclosed in { and }. In your case, AToW is a type name, not a structure specifier, so it can't be used to define an anonymous structure.

这篇关于这是C11匿名结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆