direct 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 explicit set of constructor arguments.
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 ( arg );
T object |
(1) | ||||||||
T object { arg };
T object |
(2) | (C++11 起) | |||||||
T ( other )
T |
(3) | ||||||||
static_cast< T >( other )
|
(4) | ||||||||
new T(args, ...)
|
(5) | ||||||||
Class::Class() : member(args, ...) {...
|
(6) | ||||||||
[arg](){...
|
(7) | (C++11 起) | |||||||
[编辑] 解释
在下列情况下进行直接初始化
Original:
Direct 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 with a nonempty parenthesized list of expressions
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)
在列表初始化序列,如果没有的初始化列表constuctors的提供和匹配的构造函数访问,所有必要的隐式转换,非收窄.
Original:
during 列表初始化 sequence, if no initializer-list constuctors are provided and a matching constructor is accessible, and all necessary implicit conversions are non-narrowing.
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)
初始化临时功能转换或一个括号表达式列表的一个prvalue
Original:
initialization of a prvalue temporary by 功能转换 or with a parenthesized expression 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)
一个prvalue临时一个的static_castexpession的初始化
Original:
initialization of a prvalue temporary by a 的static_cast expession
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:
initialization of an object with dynamic storage duration by a new-expression with a non-empty 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.
6)
由构造初始化列表碱或一个非静态成员的初始化
Original:
initialization of a base or a non-static member by constructor 初始化列表
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)
关闭对象的成员初始化的变量副本在一个lambda表达式捕获
Original:
initialization of closure object members from the variables caught by copy in a lambda-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.
初始化直接的影响是:1
Original:
The effects of direct initialization 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如果是一个类类型,构造函数的T进行检查,并选择最佳匹配的重载决议。然后调用该构造函数初始化对象.Original:IfTis a class type, the constructors ofTare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 否则,如果
T是一种非类类型,标准的转换被使用,如果必要的话,将该值转换为other的cv不合格版本的T.Original:Otherwise, ifTis a non-class type, 标准的转换 are used, if necessary, to convert the value of other to the cv-unqualified version ofT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 注释
直接初始化是更宽松的复制初始化:复制初始化只考虑非显式构造函数和用户定义的转换函数,而直接初始化认为所有构造函数和隐式转换序列.
Original:
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions, while direct-initialization considers all constructors and implicit conversion sequences.
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 <string> #include <iostream> #include <memory> struct Foo { int mem; explicit Foo(int n) : mem(n) {} }; int main() { std::string s1("test"); // constructor from const char* std::string s2(10, 'a'); std::unique_ptr<int> p(new int(1)); // OK: explicit constructors allowed // std::unique_ptr<int> p = new int(1); // error: constructor is explicit Foo f(2); // f is direct-initialized: // constructor parameter n is copy-initialized from the rvalue 2 // f.mem is direct-initialized from the parameter n // Foo f2 = 2; // error: constructor is explicit std::cout << s1 << ' ' << s2 << ' ' << *p << ' ' << f.mem << '\n'; }
Output:
test aaaaaaaaaa 1 2

