联合初始化列表的正确格式是什么? [英] What's the proper format for a union initializer list?
问题描述
说我有一个带有单个联合的类/结构...类似
Say I have a class/struct with a single union in it... something like:
struct Box { union { AS128 intr; struct { AS32 a, b, c, d; }; }; };
为这种数据类型创建初始化列表的正确方法是什么?
What's the proper way to do an initializer list for this kind of data type?
Box iMyBoxA = { 10, 10, 100, 100 };
Box iMyBoxB = ( Box ){ 10, 10, 100, 100 };
上面的'A'选项在许多情况下都可以使用,但是不能完全移植...给出错误没有从'括号包围的初始值设定项列表'转换参数1的错误".第二个不能编译为不能在子对象的初始化周围省略括号..."
The 'A' option above works in many cases, but isn't completely portable... giving the error "no known conversion for argument 1 from 'brace-enclosed initializer list'". And the second doesn't compile with "Cannot omit braces around initialization of subobject..."
我尝试了其他几种情况,但似乎无法使其在多个平台上进行编译.
I've tried a few other cases and can't seem to get it to compile across multiple platforms.
推荐答案
问题中结构和联合定义的原始版本为:
The original version of the structure and union definition in the question was:
struct Box { union { AS128 intr; AS32 a, b, c, d; } };
这有点不寻常.假设定义了类型AS128和AS32,则等效于:
This is a little unusual. Assuming the types AS128 and AS32 are defined, then it is equivalent to:
struct Box1 // Renamed for convenience of testing
{
union
{
AS128 intr;
AS32 a;
AS32 b;
AS32 c;
AS32 d;
};
};
也就是说,它是一个名为intr
的AS128
和四个分别称为a
,b
,c
和d
的类型AS32
的单独值的联合(后四个都是占据相同的空间).
That is, it is a union of an AS128
called intr
and 4 separate values of type AS32
called a
, b
, c
and d
(with the latter 4 all occupying the same space).
大概,您要想到的是:
struct Box2
{
union
{
AS128 intr;
struct
{
AS32 a;
AS32 b;
AS32 c;
AS32 d;
} s;
};
};
这使用C11的匿名成员功能(与C99相比,在ISO/IEC 9899:2011的前言中列为新功能). [现在已解决了该问题,以或多或少地反映此表示法(使用匿名结构).请注意,您不能直接指定匿名工会成员,因此至少在上面的s
是个好主意.可以使用不带s
的结构,但是除非您可以指定结构,否则我认为您无法初始化该结构—您只能指定并集的已命名元素.]
This uses the anonymous members feature of C11 (listed in the Foreword of ISO/IEC 9899:2011 as a new feature compared to C99). [The question has now been fixed to reflect more or less this notation (using an anonymous structure). Note that you can't directly designate an anonymous union member, so the s
above is a good idea, at least. The structure without the s
can be used, but I don't think you can initialize the structure unless you can designate it — you can only designate named elements of a union.]
使用第一个变量(struct Box1
),您可以初始化第一个没有名称的成员(AS128 intr;
成员):
With the first variant (struct Box1
), you can initialize the first member (the AS128 intr;
member) with no name:
struct Box1 b1 = { { 1234 } };
外部支架用于结构;内部括号用于(匿名)联合.
The outer braces are for the structure; the inner braces are for the (anonymous) union.
或者您可以使用指定的初始化程序指定要初始化的成员:
Or you can specify which member to initialize using a designated initializer:
struct Box1 b2 = { { .a = 1234 } };
请注意,您不能这样做:
Note that you can't do:
struct Box1 b3 = { { .a = 1234, .b = 2341 } };
编译器发出警告(请参见下面的示例代码).
The compiler gives a warning (see the example code below).
使用第二个变量(struct Box2
)和嵌套结构,您仍然可以使用未指定的初始化程序或指定的初始化程序,但是这一次,您可以指定结构的所有4个成员:
With the second variant (struct Box2
), with the nested structure, you can still use the undesignated initializer or the designated initializers, but this time, you could specify all 4 of the members of the structure:
struct Box2 b4 = { { .s = { .a = 32, .b = 65, .c = 48, .d = 97 } } };
示例代码
对AS128
和AS32
类型名称使用有趣的类型.
Example code
Using funny types for the AS128
and AS32
type names.
typedef long AS128;
typedef char AS32;
struct Box1
{
union
{
AS128 intr;
AS32 a;
AS32 b;
AS32 c;
AS32 d;
};
};
struct Box2
{
union
{
AS128 intr;
struct
{
AS32 a;
AS32 b;
AS32 c;
AS32 d;
} s;
};
};
struct Box1 b1a = { { 1234 } };
struct Box1 b1b = { { .intr = 1234 } };
struct Box1 b1c = { { .a = 32 } };
//struct Box1 b1d = { { .b = 29, .c = 31 } }; // Invalid double initialization
//error: initialized field overwritten [-Werror=override-init]
struct Box2 b2a = { { 1234 } };
struct Box2 b2b = { { .intr = 1234 } };
struct Box2 b2c = { { .s = { .a = 29, .b = 30, .c = 31, .d = 32 } } };
这篇关于联合初始化列表的正确格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!