是否允许对位字段进行汇总初始化? [英] Is Aggregate Initialization of Bit-Fields Allowed?

查看:82
本文介绍了是否允许对位字段进行汇总初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含位域的结构:

I have a struct which contains bit-fields:

struct Foo {
    unsigned a : 16, b : 16;
};

我想知道是否可以在其位字段上使用聚合初始化.例如:

And I want to know if I can use aggregate initialization on it's bit-fields. For example:

struct Foo bar = {13, 42};

我注意到,该可以在gcc 5.1和Visual Studio 2015中使用.这是标准批准的C和C ++初始化.

I note that this does work in gcc 5.1 and Visual Studio 2015. I'd just like something to certify this was standard approved initialization for both C and C++.

推荐答案

从C ++ 14 [dcl.init.aggr]开始,我们拥有

From C++14 [dcl.init.aggr] we have

集合是没有用户提供的构造函数(12.1),没有私有或受保护的非静态数据成员(第11章),没有基类(第10章)且没有虚拟的数组或类(第9章)功能(10.3).

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

因此,Foo是一个聚合,可以进行聚合初始化.然后我们有

So Foo is an aggregate an qualifies for aggregate initialization. Then we have

按照8.5.4的规定,当通过初始化程序列表初始化聚合时,初始化程序列表中的元素将按递增的下标或成员顺序作为聚合成员的初始化程序.[...]

When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order.[...]

出于聚合初始化的目的,静态数据成员和匿名位字段不被视为该类的成员.

Static data members and anonymous bit-fields are not considered members of the class for purposes of aggregate initialization.

因此,在您的情况下,它们将被初始化,因为它们不是匿名的,并且将按照它们在struct中出现的顺序进行初始化.

So in your case they will be initialized since they are not anonymous and they will be initialized in the order they appear in the struct.

根据C11 6.2.5(21),我们拥有

From C11 6.2.5(21) we have

算术类型和指针类型统称为标量类型.数组和结构类型统称为集合类型. 46 )

因此在C语言中,我们仍在处理聚合.然后在6.7.9(9)中有

So in C we are still dealing with an aggregate. Then in 6.7.9(9) we have

除非另有明确说明,否则出于本条的目的,结构和联合类型的对象的未命名成员不参与初始化.结构对象的未命名成员即使在初始化后也具有不确定的值.

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. Unnamed members of structure objects have indeterminate value even after initialization.

和6.7.9(17)

每个用大括号括起来的初始化器列表都有一个关联的当前对象.当没有 存在名称,根据当前对象的类型按顺序初始化当前对象的子对象:下标顺序递增的数组元素,声明顺序的结构成员以及并集的第一个命名成员. 148 )相反,指定会导致以下初始化程序开始对指定程序描述的子对象进行初始化.然后从指定者描述的下一个子对象开始按顺序继续初始化. 149 )

Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.148) In contrast, a designation causes the following initializer to begin initialization of the subobject described by the designator. Initialization then continues forward in order, beginning with the next subobject after that described by the designator.149)

因此,我们具有与C ++相同的行为,在C ++中,未初始化匿名位字段,但由于它们是命名的,因此将按照在struct中出现的顺序对其进行初始化.

So we have the same behavior as in C++ where anonymous bit fields are not initialized but since they are named they will be initialized in the order they appear in the struct.

这篇关于是否允许对位字段进行汇总初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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