在C1X匿名结构或联合的初始化 [英] initialization of anonymous structures or unions in C1X

查看:125
本文介绍了在C1X匿名结构或联合的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:如何是匿名结构(或联合)正确初始化根据当前的 C1X 草案?这是合法的:

I have the following question: How are anonymous structures (or unions) properly initialized according to the current C1X draft? Is this legal:

struct foo {
    int a;
    struct {
        int i;
        int j;
    };
    int b;
};

struct foo f = { 1, 2, 3, 4 };
struct foo g = { 1, { 2 }, 3 };

在GCC, GJ == 0 GB == 3 ,而台泥 GJ == 3 GB == 0 。目前的草案说:

In GCC, g.j == 0 and g.b == 3, while in tcc g.j == 3 and g.b == 0. The current draft says:

[...]结构和联合类型不参加初始化对象不愿透露姓名的成员。结构对象的未命名成员甚至初始化后有不确定的值。

"[...] unnamed members of objects of structure and union type do not participate in initialization. Unnamed members of structure objects have indeterminate value even after initialization.".

可这是真的吗?是不是

struct foo h = { 0 };

应该全部成员设置为0?

非常感谢!

更新

由于匿名成员似乎混合结构/联合时,只有有用的,如何正确初始化这样的:

Since anonymous members seem to be only useful when mixing structs/unions, how to initialize this correctly:

struct bar {
    int tag;
    union {
        double d;
        int i;
    };
};

?这使得在海湾合作委员会和LT错误; 4.6和ICC 11,但作品GCC 4.6,ICC 12,铛和TCC:

? This gives errors in gcc < 4.6 and icc 11, but works in gcc 4.6, icc 12, clang and tcc:

struct bar a = { .tag = 1, .i = 42 };

这给出铛和TCC错误,但在海湾合作委员会和ICC作品:

This gives errors in clang and tcc, but works in gcc and icc:

struct bar b = { .tag = 1, { .i = 42 } };

是第二个违反该标准的?

Is the second one a violation of the standard?

推荐答案

˚F ^ h 应该正确初始化所有成员,如 I Ĵ的像的成员被视为结构美孚(C1X 6.7.2.1§13):

f and h should correctly initialize all members, as i and j are to be treated like members of struct foo (C1x 6.7.2.1 §13):

匿名结构的成员
  或工会被认为是会员
  包含结构或联合的。

The members of an anonymous structure or union are considered to be members of the containing structure or union.

我不认为海湾合作委员会的初始化先按g 是正确的,考虑C1X 6.7.9§9:

I don't think that gcc's initialization of g is correct, considering C1x 6.7.9 §9:

除非明确说明
  否则,对于此目的
  对象的第无名成员
  结构和联合型别
  参加初始化。

Except where explicitly stated otherwise, for the purposes of this subclause unnamed members of objects of structure and union type do not participate in initialization.

§20 - 这与次级总交易 - 包含有关这一问题没有明确的说法,所以我最好的猜测是,适用§9(但仅限于总本身的的其会员!)。

§20 - which deals with sub-aggregates - contains no explicit statement relevant to the issue, so my best guess would be that §9 applies (but only to the aggregate itself, not to its members!).

底线是匿名子聚集不存在作为单独的对象,即TCC的行为应该是正确的...

The bottom line is that anonymous sub-aggregates don't exist as separate objects, ie tcc's behaviour should be correct...

举例code我就此问题:

Example code for my take on the issue:

struct foo
{
    struct bar { int i; }; // (1) unnamed, but tagged, ie *not* anonymous
    struct { int j; };     // (2) unnamed, but anonymous
    struct { int k; } baz; // (3) named, but not tagged
};

(1)发生在初始化的任何部分;(2)初始化仿佛结构美孚有一个额外的成员名为Ĵ,(3)初始化作为一个经常分总和。

(1) takes no part in initialization, (2) initializes as though struct foo had an additional member named j, (3) initializes as a regular sub-aggregate.

如果我间pretation是正确的,如果包含在联盟内的匿名结构才有意义:结构内的匿名结构含有额外的成员扁平结构没有区别

If my interpretation is correct, anonymous structures only make sense if contained within a union: an anonymous structure within a structure is indistinguishable from a flat structure containing additional members.

这篇关于在C1X匿名结构或联合的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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