T v {}初始化 [英] T v{} initialization
问题描述
我正在阅读C ++ 11标准,但无法确定
I'm reading the C++11 standard, but can't figure out whether
T x{};
是值初始化或默认初始化(自动存储)。
表示:
is value-initialized or default initialized (automatic storage). It does say pretty clearly that:
10初始值为空的对象括号,即(),应进行值初始化。
10 An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.
而
11如果没有为对象指定初始化器,则对象将被默认初始化;
11 If no initializer is specified for an object, the object is default-initialized;
但我能找到 T x {};
是:
初始化发生在形式T x(a);
T x {a};
以及新表达式(5.3.4),static_cast表达式(5.2.9),功能符号类型转换(5.2.3)和基本和成员初始化器(12.6.2)被称为直接初始化。
The initialization that occurs in the forms T x(a); T x{a}; as well as in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions (5.2.3), and base and member initializers (12.6.2) is called direct-initialization.
和
(非圆括号)支撑初始化列表,对象或引用是列表初始化的(8.5.4)。
If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).
新的潜水到阅读标准的水平。
I'm new to diving into the level of reading the standards. Can someone point me in the right direction?
推荐答案
这是您的报价确实涵盖的:
This is indeed covered by your quote:
如果初始化程序是(非括号) braced-init-list ,则对象或引用将被列表初始化(8.5.4)
If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).
跳到8.5.4列表初始化。这里我改写/省略了一些与 T x {}
的情况无关的点:
Skipping down to 8.5.4 List-initialization. Here I have paraphrased/omitted some points that don't pertain to the case of T x{}
:
类型T的对象或引用的列表初始化定义如下:
List-initialization of an object or reference of type T is defined as follows:
- 如果T是聚集
- 否则,如果初始化器列表没有元素,T是带有默认构造函数的类类型,则对象将被初始化为 li>
- 否则,如果
T
是std :: initializer_list< E>
[...] - 否则,[如果列表不为空并且它与构造函数匹配]
- 否则,[如果列表有单元素]
- 否则,[if
T
是引用类型] - 如果初始化器列表没有元素,则该对象是值初始化的。
- 否则,程序是错误的。
- If T is an aggregate, aggregate initialization is performed (8.5.1).
- Otherwise, if the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized
- Otherwise, if
T
is a specialization ofstd::initializer_list<E>
[...] - Otherwise, [if the list is not empty and it matches a constructor]
- Otherwise, [if the list has a single element]
- Otherwise, [if
T
is a reference type] - Otherwise, if the initializer list has no elements, the object is value-initialized.
- Otherwise, the program is ill-formed.
第一点,聚集初始化也在C ++ 03中;在这种情况下 T x {};
与 T x = {};
相同。
The first point, aggregate initialization was in C++03 as well; in that case T x{};
is the same as T x = {};
.
对于第二点T是具有默认构造函数的类类型,它是值初始化,意味着调用默认构造函数。
For the second point "T is a class type with a default constructor", it is value-initialized which means calling the default constructor.
如果 T
是原始类型,则第二个到最后一个点适用,并且它是 >。
If T
is a primitive type then the second-to-last point applies and it is value-initialized again.
返回到集合初始化情况,在8.5.1 / 7中有:
Going back to the aggregate initialization case, in 8.5.1/7 there is:
如果列表中的初始化子句数少于聚合中的成员数,则未明确初始化的每个成员都应从它的大括号或初始值初始化或者,如果没有括号或等号初始化器,则从空初始化器列表(8.5.4)。
If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal-initializer, from an empty initializer list (8.5.4).
括号或初始化器是指类定义中内联的初始化器。如果不存在,那么它被初始化,如同成员已经用 {}
初始化了(所以,这个逻辑被递归地应用于每个聚合成员)。
The brace-or-equal-initializer refers to an initializer provided inline in the class definition. If that isn't present then it is initialized as if the member had been initialized with {}
(so, this logic is recursively applied for each aggregate member).
例如,
struct T
{
int a;
};
然后 T x {};
a 被初始化为
int a {};
,这是值初始化,因为 int
是原始类型。
then T x {};
leads to a
being initialized as if it were int a{};
, which is value-initialization since int
is a primitive type.
这篇关于T v {}初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!