名前空間
変種
操作

list initialization

提供: cppreference.com
< cpp‎ | language

 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
ブレース-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.

目次

[編集] 構文

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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
タイプ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.
  • ブレース-initはリストが空であるとTはデフォルトコンストラクタを持つクラス型である場合、値初期化が実行されます.
    Original:
    If the braced-init-list is empty and T 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, if T 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.
  • そうでない場合、Tstd::initializer_listの専門化が、同じタイプの新しいstd::initializer_listオブジェクトの構築と型Tのオブジェクトを直接初期化したり、コピー&初期化するために使用される、コンテキストに応じてされている.
    Original:
    Otherwise, if T is a specialization of std::initializer_list, a new std::initializer_list object of the same type is constructed and used to direct-initialize or copy-initialize the object of type T, 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 of T 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_list
    The 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 of T 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, if T 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.
  • 整数型、浮動小数点型から変換
    Original:
    conversion from a floating-point type to an integer type
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ソースには、値がターゲットの型で正確に記憶することができる定数式である場合を除きlong doubleからdoubleへまたはfloatdoubleから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 type
    The 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 type
    The 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.

[編集]

#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


[編集] も参照してください