名前空間
変種
操作

aggregate initialization

提供: cppreference.com
< cpp‎ | language

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外指定 (非推奨)
noexcept 指定子 (C++11)
例外
名前空間
指定子
decltype (C++11)
auto (C++11)
alignas (C++11)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点
文字 - 文字列 - nullptr (C++11)
ユーザ定義 (C++11)
ユーティリティ
属性 (C++11)
typedef 宣言
型エイリアス宣言 (C++11)
キャスト
暗黙の変換 - 明示的な変換
static_cast - dynamic_cast
const_cast - reinterpret_cast
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
テンプレート
その他
 
ブレース-initはリストから集計を初期化します
Original:
Initializes an aggregate 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 object {arg1, arg2, ...}; (2) (C++11およびそれ以降)

[編集] 説明

集約の初期化は、'集合体を初期化リスト初期化のフォームです
Original:
Aggregate initialization is a form of リスト初期化, which initializes aggregates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
集計は、次のいずれかです。型のオブジェクトです
Original:
An aggregate is an object of the type that is one of 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:
    array type
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • クラス型(典型的には、structまたはunion)、それは持っています
    Original:
    class type (typically, struct or union), that has
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ノープライベートまたはプロテクトメンバー
    Original:
    no private or protected members
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • は、ユーザーが提供するコンストラクタがありません
    Original:
    no user-provided constructors
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ない基底クラスはありません
    Original:
    no base classes
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ノー仮想メンバ関数
    Original:
    no virtual 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:
    no brace-or-equal initializers for non-static members
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
集約の初期化の効果は、次のとおりです
Original:
The effects of aggregate initialization are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • 各配列要素または非静的クラスのメンバは、クラス定義の中に配列の添字/外観のために、初期化子リストの対応する節からコピー初期化です.​​
    Original:
    Each array element or non-static class member, in order of array subscript/appearance in the class definition, is コピー初期化 from the corresponding clause of the initializer list.
    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:
    If the initializer clause is an expression, implicit conversions are allowed, except (C++11およびそれ以降) if they are narrowing (as in リスト初期化).
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 初期化子の句は、ネストされたブレース-INIT-リストである場合、対応するクラスのメンバーは、それ自体である集計:集計初期化は再帰的です.
    Original:
    If the initializer clause is a nested braced-init-list, the corresponding class member is itself an aggregate: aggregate initialization is recursive.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • オブジェクトが未知のサイズの配列であり、供給中括弧で囲まれた初期化子リストはn句を持っている場合は、配列のサイズはnです
    Original:
    If the object is an array of unknown size, and the supplied brace-enclosed initializer list has n clauses, the size of the array is n
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 静的データメンバと匿名のビットフィールドは、集約の初期化中にスキップされます.
    Original:
    Static data members and anonymous bit-fields are skipped during aggregate initialization.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 初期化子の句の数は初期化す​​るメンバーの数を超えている場合、プログラムは(コンパイラエラー)不適格です
    Original:
    If the number of initializer clauses exceeds the number of members to initialize, the program is ill-formed (compiler error)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 初期化子の句の数はメンバーの数より少ない場合、残りのメンバーは値初期化を行い、空のリストで初期化されます。参照型のメンバは、これらの残りのメンバーの一人である場合、プログラムは不適格である(参照は値初期化することはできません)
    Original:
    If the number of initializer clauses is less than the number of members, the remaining members are initialized by empty lists, which performs 値初期化. If a member of a reference type is one of these remaining members, the program is ill-formed (references cannot be value-initialized)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 集約の初期化は、等号(T a = {args..})でフォームを使用する場合は、ネストされた初期化子リストを囲む中括弧は、その場合には、必要な数の初期化句が対応のすべてのメンバーまたは要素を初期化するために使用され、(中略)削除されるかもしれませんsubaggregate、その後の初期化子の句は、オブジェクトの次のメンバを初期化するために使用されます。オブジェクトがどのメンバー(空の構造体、または静的メンバのみを保持する構造体)をせずにサブ集合を持っている場合は、中括弧省略は許されず、空のネストされたリスト{} を使用しなければなりません.
    Original:
    If the aggregate initialization uses the form with the equal sign (T a = {args..}), the braces around the nested initializer lists may be elided (omitted), in which case, as many initializer clauses as necessary are used to initialize every member or element of the corresponding subaggregate, and the subsequent initializer clauses are used to initialize the following members of the object. However, if the object has a sub-aggregate without any members (an empty struct, or a struct holding only static members), brace elision is not allowed, and an empty nested list {} must be used.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 組合が集約の初期化で初期化される時にのみ、その最初の非静的データメンバが初期化され.
    Original:
    When a union is initialized by aggregate initialization, only its first non-static data members is initialized.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[編集] ノート

℃まで+ 11、縮小変換は、集約の初期化では許可されていましたが、これは許可されなくなりました.
Original:
Until C++11, narrowing conversions were permitted in aggregate initialization, but they are no longer allowed.
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:
Until C++11, aggregate initialization could not be used in a constructor initializer list due to syntax restrictions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

#include <string>
#include <array>
struct S {
    int x;
    struct Foo {
        int i;
        int j;
        int a[3];
    } b;
};
 
union U {
    int a;
    const char* b;
};
int main()
{
    S s1 = { 1, { 2, 3, {4, 5, 6} } };
    S s2 = { 1, 2, 3, 4, 5, 6}; // same, but with brace elision
    S s3{1, {2, 3, {4, 5, 6} } }; // same, using direct-list-initialization syntax
//  S s4{1, 2, 3, 4, 5, 6}; // error: brace-elision only allowed with equals sign
 
    int ar[] = {1,2,3}; // ar is int[3]
//  char cr[3] = {'a', 'b', 'c', 'd'}; // too many initializer clauses
    char cr[3] = {'a'}; // array initialized as {'a', '\0', '\0'}
 
    int ar2d1[2][2] = {{1, 2}, {3, 4}}; // fully-braced 2D array: {1, 2}
                                        //                        {3, 4}
    int ar2d2[2][2] = {1, 2, 3, 4}; // brace elision: {1, 2}
                                    //                {3, 4}
    int ar2d3[2][2] = {{1}, {2}};   // only first column: {1, 0}
                                    //                    {2, 0}
 
    std::array<int, 3> std_ar2{ {1,2,3} };    // std::array is an aggregate
    std::array<int, 3> std_ar1 = {1, 2, 3}; // brace-elision okay
 
    int ai[] = { 1, 2.0 }; // narrowing conversion from double to int:
                           // error in C++11, okay in C++03
 
    std::string ars[] = {std::string("one"), // copy-initialization
                         "two",              // conversion, then copy-initialization
                         {'t', 'h', 'r', 'e', 'e'} }; // list-initialization
 
    U u1 = {1}; // OK, first member of the union
//    U u2 = { 0, "asdf" }; // error: too many initializers for union
//    U u3 = { "asdf" }; // error: invalid conversion to int
 
}


[編集] 参照