list initialization
提供: cppreference.com
![]() |
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
ブレース-initのリストからオブジェクトを初期化します
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)
式またはネストされたリストの括弧で囲まれたリスト(ブレース-INIT-リスト)と命名された変数の初期化
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)
ブレース-INIT-リストを使用できる無名の一時の初期化
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)
初期化子がブレースのinit-listですnew式とダイナミック記憶域期間を持つオブジェクトの初期化
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)
リターン式として使用ブレース-initの-listで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)
関数呼び出し式で、引数として使用ブレース-initの-listで
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)
機能的なキャスト式またはその他の直接初期化で、コンストラクタの引数として使用されるブレース-initの-listで
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.
- ブレース-initはリストが空であると
T
はデフォルトコンストラクタを持つクラス型である場合、値初期化が実行されます.Original:If the braced-init-list is empty andT
is 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, ifT
is 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.
- そうでない場合、
T
std::initializer_listの専門化が、同じタイプの新しいstd::initializer_list
オブジェクトの構築と型T
のオブジェクトを直接初期化したり、コピー&初期化するために使用される、コンテキストに応じてされている.Original: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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- それ以外の場合は、
T
のとるコンストラクタは2つのフェーズで、考慮されています:Original:Otherwise, the constuctors ofT
are 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
のすべてのコンストラクタだけ縮小変換が許可されていることを制限付きで、ブレース-INIT-listの要素から構成されて一連の引数に対してオーバーロードの解決に参加しています。このステージは、コピーリスト初期化のベストマッチとしてコンパイル失敗を明示的なコンストラクタを生成した場合(ただし、単純なコピー初期化では、明示的なコンストラクタは全く考慮されません)Original: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)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, ifT
is 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.
- それ以外の場合は、ブレース-initのリストが1つだけの要素を持っている場合、
T
です直接初期化されますまたはコピー初期化、縮小変換が許可されていないことを除けば、文脈に応じて.Original:Otherwise, if the braced-init-list has only one element,T
is 直接初期化されます 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.
- それ以外の場合は、ブレース-initはリストに要素がない場合、
T
です値に初期化された.Original:Otherwise, if the braced-init-list has no elements,T
is 値に初期化された.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.
[編集] ノート
ブレース-INIT-リスト表現ではありませんし、独自には何種類もありません:例えば、関数テンプレートを呼び出すときに、ブレース-INIT-list引数は、テンプレートの型推論に使用することはできません。特別な例外として任意autoブレース-INIT-リストを推測キーワード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 << ' '; }
出力:
world 0 1 1 abcd cd aa 1 a 2 abc 3 abcd 1 2 3 -1 -2 -3