aggregate initialization
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
从支撑初始化列表初始化总和
原文:
Initializes an aggregate from braced-init-list
目录 |
[编辑] 语法
T object = { arg1, arg2, ...};
|
(1) | ||||||||
T object { arg1, arg2, ...};
|
(2) | (C++11 起) | |||||||
[编辑] 解释
集合初始化列表初始化,它初始化“聚集”的一种形式
原文:
Aggregate initialization is a form of 列表初始化, which initializes aggregates
聚集是对象的类型,它是下列之一
原文:
An aggregate is an object of the type that is one of the following
- 数组类型
- 类类型(通常情况下,struct或union)原文:class type (typically, struct or union), that has
- 没有私有或受保护的成员
- 没有用户提供的构造函数
- 没有基类
- 没有虚拟成员函数
- 没有括号或等于非静态成员的初始化原文:no brace-or-equal initializers for non-static members
-
集合初始化的效果是:1
原文:
The effects of aggregate initialization are:
- 如果初始子句是一个嵌套的支撑初始化列表,相应的类成员本身就是一个总的集合初始化是递归的.原文:If the initializer clause is a nested braced-init-list, the corresponding class member is itself an aggregate: aggregate initialization is recursive.
- 如果对象是一个数组的大小未知,所提供的括号内的初始化列表中有
n
条款,数组的大小是n
原文:If the object is an array of unknown size, and the supplied brace-enclosed initializer list hasn
clauses, the size of the array isn
- 期间,跳过集合初始化静态数据成员和匿名位域.原文:Static data members and anonymous bit-fields are skipped during aggregate initialization.
- 如果数的初始值设定项条款的数量超过了成员初始化,该方案是生病形成(编译器错误)原文:If the number of initializer clauses exceeds the number of members to initialize, the program is ill-formed (compiler error)
- 如果数的初始值设定项条款是小于的成员数量,剩余的成员初始化为空列表,执行值初始化。如果一个引用类型的成员是这些剩余的成员之一,该计划是格式错误的(参考值不能初始化)原文:If the number of initializer clauses is less than the number of members, the remaining members are initialized by empty lists, which performs 值初始化. If a member of a reference type is one of these remaining members, the program is ill-formed (references cannot be value-initialized)
- 如果集合初始化使用的形式等号(T a = {args..}),嵌套的大括号初始化列表可能会被忽略掉(略),在这种情况下,许多初始值设定项条款,在必要时用于初始化每名成员或元素所对应的子聚合,并在随后的初始值设定项条款被用来初始化对象的成员。但是,如果该对象没有任何成员(空结构,或只持有静态成员的结构)的一个子集料,括号省略是不允许的,必须使用一个空的嵌套列表
{}
.原文:If the aggregate initialization uses the form with the equal sign (T a = {args..}), the braces around the nested initializer lists may be elided (omitted), in which case, as many initializer clauses as necessary are used to initialize every member or element of the corresponding subaggregate, and the subsequent initializer clauses are used to initialize the following members of the object. However, if the object has a sub-aggregate without any members (an empty struct, or a struct holding only static members), brace elision is not allowed, and an empty nested list{}
must be used.
- 当工会是第一个非静态数据成员初始化集合初始化,初始化.原文:When a union is initialized by aggregate initialization, only its first non-static data members is initialized.
[编辑] 注释
在C + +11之前,缩小转换被允许在集合初始化,但他们不再允许.
原文:
Until C++11, narrowing conversions were permitted in aggregate initialization, but they are no longer allowed.
C + +11之前,不能使用集合初始化构造函数初始化列表中,由于语法限制.
原文:
Until C++11, aggregate initialization could not be used in a constructor initializer list due to syntax restrictions.
[编辑] 示例
#include <string> #include <array> struct S { int x; struct Foo { int i; int j; int a[3]; } b; }; union U { int a; const char* b; }; int main() { S s1 = { 1, { 2, 3, {4, 5, 6} } }; S s2 = { 1, 2, 3, 4, 5, 6}; // same, but with brace elision S s3{1, {2, 3, {4, 5, 6} } }; // same, using direct-list-initialization syntax // S s4{1, 2, 3, 4, 5, 6}; // error: brace-elision only allowed with equals sign int ar[] = {1,2,3}; // ar is int[3] // char cr[3] = {'a', 'b', 'c', 'd'}; // too many initializer clauses char cr[3] = {'a'}; // array initialized as {'a', '\0', '\0'} int ar2d1[2][2] = {{1, 2}, {3, 4}}; // fully-braced 2D array: {1, 2} // {3, 4} int ar2d2[2][2] = {1, 2, 3, 4}; // brace elision: {1, 2} // {3, 4} int ar2d3[2][2] = {{1}, {2}}; // only first column: {1, 0} // {2, 0} std::array<int, 3> std_ar2{ {1,2,3} }; // std::array is an aggregate std::array<int, 3> std_ar1 = {1, 2, 3}; // brace-elision okay int ai[] = { 1, 2.0 }; // narrowing conversion from double to int: // error in C++11, okay in C++03 std::string ars[] = {std::string("one"), // copy-initialization "two", // conversion, then copy-initialization {'t', 'h', 'r', 'e', 'e'} }; // list-initialization U u1 = {1}; // OK, first member of the union // U u2 = { 0, "asdf" }; // error: too many initializers for union // U u3 = { "asdf" }; // error: invalid conversion to int }