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)
リスト初期化シーケンスの間、全く初期化リストとるコンストラクタが提供されていないと一致するコンストラクターがアクセス可能であるし、必要なすべての暗黙的な変換が非縮めている場合.
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)
static_castをのexpessionによる一時prvalueの初期化
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)
ラムダ式の中でキャッチコピーの変数からクロージャオブジェクトのメンバの初期化
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.
直接初期化の効果は、次のとおりです
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.
[編集] ノート
直接初期化は、コピー初期化よりも寛容です。copy-初期直接初期化はすべてのコンストラクタと暗黙の変換シーケンスを考慮しているだけに、非明示的なコンストラクタやユーザ定義の変換関数を考慮.
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