◐ Shell
clean mode source ↗

aggregate initialization — cppreference.com

<metanoindex/>

Initialise un total de contreventement-init-list

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.

Syntaxe

T object = {arg1, arg2, ...}; (1)
T object {arg1, arg2, ...}; (2) (depuis C++11)

Explication

Initialisation par agrégat est une forme de liste d'initialisation, qui initialise agrégats'

Original:

Aggregate initialization is a form of liste d'initialisation, 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.

Un agrégat est un objet du type qui est l'un des suivants

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.

  • type de tableau

    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.

  • type de classe (généralement, struct ou union), qui a

    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.

  • pas de membres privés ou protégés

    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.

  • pas de constructeurs fournis par l'utilisateur

    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.

  • pas de classes de base

    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.

  • pas de fonctions membres virtuelles

    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.

  • pas d 'initialiseur brace-ou-égal pour les non-membres statiques

    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.

Les effets de l'initialisation d'agrégats sont les suivants:

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.

  • Chaque élément du tableau ou de membre de classe non statique, afin d'indice de tableau / apparition dans la définition de classe, est copie-initialisé de la clause correspondante de la liste d'initialisation .

    Original:

    Each array element or non-static class member, in order of array subscript/appearance in the class definition, is copie-initialisé 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.

  • Si la clause d'initialisation est une expression, les conversions implicites sont autorisées, sauf si elles sont (depuis C++11) rétrécissement (comme dans liste d'initialisation) .

    Original:

    If the initializer clause is an expression, implicit conversions are allowed, except (depuis C++11) if they are narrowing (as in liste d'initialisation).

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • Si la clause d'initialisation est un imbriquée contreventés-init-liste, le membre de la classe correspondante est elle-même un agrégat: initialisation par agrégat est récursive .

    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.

  • Si l'objet est un tableau de taille inconnue, et la liste d'initialisation fourni brace-clos a n clauses, la taille du tableau est 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.

  • Membres de données statiques et anonymes bit-champs sont ignorés lors de l'initialisation globale .

    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.

  • Si le nombre de clauses initialiseur dépasse le nombre de membres pour initialiser, le programme est mal formé (erreur de compilation)

    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.

  • Si le nombre de clauses initialiseur est inférieur au nombre de membres, les membres restants sont initialisés par des listes vides, qui effectue la valeur d'initialisation. Si un membre d'un type de référence est l'un de ces membres restants, le programme est mal formé (références ne peut pas être initialisé valeur)

    Original:

    If the number of initializer clauses is less than the number of members, the remaining members are initialized by empty lists, which performs la valeur d'initialisation. 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.

  • Si l'initialisation globale utilise le formulaire avec le signe égal (T a = {args..}), les accolades autour des listes imbriquées initialiseur peut être élidé (omis), dans ce cas, que les clauses initialiseur que nécessaire sont utilisés pour initialiser chaque membre ou d'un élément du correspondant sous-agrégat, et les clauses initialiseur successifs sont utilisés pour initialiser les membres suivants de l'objet. Toutefois, si l'objet a un sous-agrégat sans membres (une struct vide, ou une structure tenant uniquement des membres statiques), élision orthèse n'est pas permis, et une liste vide {} imbriquée doit être utilisé .

    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.

  • Lorsqu'un syndicat est initialisé par l'initialisation globale, seuls ses premiers membres de données non statiques est initialisée .

    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.

Notes

Jusqu'à ce que C + +11, les conversions restrictives ont été autorisés dans l'initialisation globale, mais ils ne sont plus autorisés .

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.

Jusqu'à ce que C + +11, initialisation par agrégat ne peut pas être utilisé dans une liste d'initialisation du constructeur en raison des restrictions de syntaxe .

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.

Exemple

#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

}

Voir aussi