list initialization
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
从支撑初始化列表初始化对象
Original:
Initializes an object from braced-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 语法
T object { arg1, arg2, ... };
|
(1) | ||||||||
T { arg1, arg2, ... };
|
(2) | ||||||||
new T { arg1, arg2, ... };
|
(3) | ||||||||
return { arg1, arg2, ... } ;
|
(4) | ||||||||
function( { arg1, arg2, ... } ) ;
|
(5) | ||||||||
object[ { arg1, arg2, ... } ] ;
|
(6) | ||||||||
T( { arg1, arg2, ... } )
|
(7) | ||||||||
Class { T member = { arg1, arg2, ... }; };
|
(8) | ||||||||
Class::Class() : member{arg1, arg2, ...} {...
|
(9) | ||||||||
T object = {arg1, arg2, ...};
|
(10) | ||||||||
[编辑] 解释
列表中进行初始化的以下几种情况:
Original:
List initialization is performed in the following situations:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
一个大括号内的表达式或嵌套列表(列表中的支撑初始化列表)命名的变量的初始化
Original:
initialization of a named variable with a brace-enclosed list of expressions or nested lists (braced-init-list)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
一位不愿透露姓名的临时用一个支撑初始化列表初始化
Original:
initialization of an unnamed temporary with a braced-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
初始化一个新的表达,初始值是一个大括号初始化列表的动态存储持续时间的对象
Original:
initialization of an object with dynamic storage duration with a new-expression, where the initializer is a brace-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
在return语句,返回表达式作为支撑初始化列表
Original:
in a return statement with braced-init-list used as the return expression
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
5)
在一个函数调用表达式,支撑初始化列表作为一个参数
Original:
in a function call expression, with braced-init-list used as an argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
6)
在一个标表达式与一个用户定义运算符[]
Original:
in a subscript expression with a user-defined operator[]
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
7)
强制转换表达式或其他直接初始化功能,支撑初始化列表作为构造函数的参数
Original:
in a functional cast expression or other direct-initialization, with braced-init-list used as the constructor argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
8)
在非静态数据成员的初始化
Original:
in a non-static data member initializer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
9)
在构造函数初始化列表
Original:
in a constructor initializer list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
10)
的右手侧的等号(类似于复制初始化)
Original:
on the right-hand-side of the equals sign (similar to 复制初始化)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
的列表的初始化的对象类型
T的影响是:Original:
The effects of list initialization of an object of type
T are:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 如果
T支撑初始化列表是空的,是一个默认的构造函数的类类型,值初始化进行.Original:If the braced-init-list is empty andTis a class type with a default constructor, 值初始化 is performed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 否则,如果
T是一个聚合类型,集合初始化进行.Original:Otherwise, ifTis an aggregate type, 集合初始化 is performed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 否则,如果
Tstd::initializer_list是一个特例,一个新std::initializer_list相同类型的对象被构造用于指示初始化或复制初始化的对象的类型T,这取决于上下文.Original:Otherwise, ifTis a specialization of std::initializer_list, a newstd::initializer_listobject of the same type is constructed and used to direct-initialize or copy-initialize the object of typeT, depending on context.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 否则,被认为是的constuctors的
T,分两个阶段进行Original:Otherwise, the constuctors ofTare considered, in two phases:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- std::initializer_list作为唯一的参数,或作为第一个参数,如果其余的参数有默认值,所有构造进行检查,和对一个参数的类型std::initializer_list相匹配的重载决议Original:All constructors that take std::initializer_list as the only argument, or as the first argument if the remaining arguments have default values, are examined, and matched by overload resolution against a single argument of type std::initializer_listThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- 如果前面的阶段没有产生匹配,所有构造函数的
T参与重载决策支撑初始化列表中的元素,它包含的参数与设定的限制,只有缩小转换是允许的。如果这个阶段产生一个明确的构造函数作为最佳匹配的副本列表初始化,编译失败(请注意,在简单的复制初始化,显式构造函数不考虑)Original:If the previous stage does not produce a match, all constructors ofTparticipate in overload resolution against the set of arguments that consists of the elements of the braced-init-list, with the restriction that only narrowing conversions are allowed. If this stage produces an explicit constructor as the best match for a copy-list-initialization, compilation fails (note, in simple copy-initialization, explicit constructors are not considered at all)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- 否则,如果
T是引用类型引用的类型,prvalue临时列表初始化,和引用绑定到该临时.Original:Otherwise, ifTis reference type, a prvalue temporary of the referenced type is list-initialized, and the reference is bound to that temporary.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 否则,如果支撑初始化列表只有一个元素,
T直接初始化或复制初始化,根据上下文,除了缩小转换不允许.Original:Otherwise, if the braced-init-list has only one element,Tis 直接初始化 or 复制初始化, depending on context, except that narrowing conversions are not allowed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 否则,如果支撑初始化列表中没有的元素,
T是值初始化.Original:Otherwise, if the braced-init-list has no elements,Tis 值初始化.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 收缩转换
列表初始化限制允许隐式转换通过禁止以下操作:
Original:
list-initialization limits the allowed 隐式转换 by prohibiting the following:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 从浮点类型为整数类型的转换Original:conversion from a floating-point type to an integer typeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- long double转换double或float和double转换float,除了源是完全可以存储在目标类型的常量表达式的值Original:conversion from a long double to double or to float and conversion from double to float, except where the source is a constant expression whose value can be stored exactly in the target typeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 整数或无作用域的枚举类型转换为整数类型,不能代表所有的原始值,但其中源是一个常量表达式的值,完全可以存储在目标类型Original:conversion from integer or unscoped enumeration type to integer type that cannot represent all values of the original, except where source is a constant expression whose value can be stored exactly in the target typeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 注释
斜撑初始化列表是一个表达式,有没有自己的类型:例如,当调用一个函数模板,支撑初始化列表参数不能用于模板类型推演。一个特殊的例外是auto的关键字,推导出任何支撑的初始化列表中std::initializer_list.
Original:
Braced-init-list is not an expression and has no type on its own: for example, when calling a function template, braced-init-list argument cannot be used for template type deduction. A special exception is made for the keyword auto, which deduces any braced-init-list as std::initializer_list.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 为例
#include <iostream> #include <vector> #include <map> #include <string> struct Foo { std::vector<int> mem = {1,2,3}; // list-initialization of a non-static member std::vector<int> mem2; Foo() : mem2{-1, -2, -3} {} // list-initialization of a member in constructor }; std::pair<std::string, std::string> f(std::pair<std::string, std::string> p) { return {p.second, p.first}; // list-initialization in return statement } int main() { int n0{}; // value-initialization (to zero) int n1{1}; // direct-list-initialization std::string s1{'a', 'b', 'c', 'd'}; // initializer-list constructor call std::string s2{s1, 2, 2}; // regular constructor call std::string s3{0x61, 'a'}; // initializer-list ctor is preferred to (int, char) int n2 = {1}; // copy-list-initialization double d = double{1.2}; // list-initialization of a temporary, then copy-init std::map<int, std::string> m = { // nested list-initialization {1, "a"}, {2, {'a', 'b', 'c'} }, {3, s1} }; std::cout << f({"hello", "world"}).first // list-initialization in function call << '\n'; const int (&ar)[2] = {1,2}; // binds a lvalue reference to a temporary array int&& r1 = {1}; // binds a rvalue reference to a temporary int // int& r2 = {2}; // error: cannot bind rvalue to a non-const lvalue ref // int bad{1.0}; // error: narrowing conversion unsigned char uc1{10}; // okay // unsigned char uc2{-1}; // error: narrowing conversion Foo f; std::cout << n0 << ' ' << n1 << ' ' << n2 << '\n' << s1 << ' ' << s2 << ' ' << s3 << '\n'; for(auto p: m) std::cout << p.first << ' ' << p.second << '\n'; for(auto n: f.mem) std::cout << n << ' '; for(auto n: f.mem2) std::cout << n << ' '; }
Output:
world 0 1 1 abcd cd aa 1 a 2 abc 3 abcd 1 2 3 -1 -2 -3

