list initialization
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
从支撑初始化列表初始化对象
原文:
Initializes an object from braced-init-list
目录 |
[编辑] 语法
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) | ||||||||
[编辑] 解释
列表中进行初始化的以下几种情况:
原文:
List initialization is performed in the following situations:
1)
一个大括号内的表达式或嵌套列表(列表中的支撑初始化列表)命名的变量的初始化
原文:
initialization of a named variable with a brace-enclosed list of expressions or nested lists (braced-init-list)
2)
一位不愿透露姓名的临时用一个支撑初始化列表初始化
原文:
initialization of an unnamed temporary with a braced-init-list
3)
初始化一个新的表达,初始值是一个大括号初始化列表的动态存储持续时间的对象
原文:
initialization of an object with dynamic storage duration with a new-expression, where the initializer is a brace-init-list
4)
在return语句,返回表达式作为支撑初始化列表
原文:
in a return statement with braced-init-list used as the return expression
5)
在一个函数调用表达式,支撑初始化列表作为一个参数
原文:
in a function call expression, with braced-init-list used as an argument
6)
在一个标表达式与一个用户定义运算符[]
原文:
in a subscript expression with a user-defined operator[]
7)
强制转换表达式或其他直接初始化功能,支撑初始化列表作为构造函数的参数
原文:
in a functional cast expression or other direct-initialization, with braced-init-list used as the constructor argument
8)
在非静态数据成员的初始化
原文:
in a non-static data member initializer
9)
在构造函数初始化列表
的列表的初始化的对象类型
T
的影响是:原文:
The effects of list initialization of an object of type
T
are:- 否则,如果
T
std::initializer_list是一个特例,一个新std::initializer_list
相同类型的对象被构造用于指示初始化或复制初始化的对象的类型T
,这取决于上下文.原文:Otherwise, ifT
is a specialization of std::initializer_list, a newstd::initializer_list
object of the same type is constructed and used to direct-initialize or copy-initialize the object of typeT
, depending on context.
- 否则,被认为是的constuctors的
T
,分两个阶段进行原文:Otherwise, the constuctors ofT
are considered, in two phases:
- std::initializer_list作为唯一的参数,或作为第一个参数,如果其余的参数有默认值,所有构造进行检查,和对一个参数的类型std::initializer_list相匹配的重载决议原文: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_list
-
- 如果前面的阶段没有产生匹配,所有构造函数的
T
参与重载决策支撑初始化列表中的元素,它包含的参数与设定的限制,只有缩小转换是允许的。如果这个阶段产生一个明确的构造函数作为最佳匹配的副本列表初始化,编译失败(请注意,在简单的复制初始化,显式构造函数不考虑)原文:If the previous stage does not produce a match, all constructors ofT
participate 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)
-
- 否则,如果
T
是引用类型引用的类型,prvalue临时列表初始化,和引用绑定到该临时.原文:Otherwise, ifT
is reference type, a prvalue temporary of the referenced type is list-initialized, and the reference is bound to that temporary.
[编辑] 收缩转换
- 从浮点类型为整数类型的转换原文:conversion from a floating-point type to an integer type
- long double转换double或float和double转换float,除了源是完全可以存储在目标类型的常量表达式的值原文: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 type
- 整数或无作用域的枚举类型转换为整数类型,不能代表所有的原始值,但其中源是一个常量表达式的值,完全可以存储在目标类型原文: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 type
[编辑] 注释
斜撑初始化列表是一个表达式,有没有自己的类型:例如,当调用一个函数模板,支撑初始化列表参数不能用于模板类型推演。一个特殊的例外是auto的关键字,推导出任何支撑的初始化列表中std::initializer_list.
原文:
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.
[编辑] 示例
#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 << ' '; }
输出:
world 0 1 1 abcd cd aa 1 a 2 abc 3 abcd 1 2 3 -1 -2 -3