[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/language/list_initialization [Back]  [Original]

list initialization cppreference.com
cppreference.com
Espaces de noms
Variantes

list initialization

De cppreference.com

<metanoindex/>

 
 
Langage C++
Sujets gnraux
Contrle de flux
Instructions conditionnelles
Instructions d'itration
Instructions de saut
Fonctions
dclaration de fonction
expression lambda
fonction gnrique
spcificateur inline
spcification d'exception (obsolte)
spcificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spcificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littraux
Expressions
oprateurs alternatifs
Utilitaires
Types
dclaration typedef
dclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mmoire
Classes
Qualificatifs spcifiques aux membres de classe
Fonctions membres spciales
Modles
classes gnriques
fonctions gnriques
spcialisation de modles
paquets de paramtres (C++11)
Divers
Assembleur
 
Initialise un objet de contrevent-init-list
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.

Syntaxe

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)

Explication

Initialisation de la liste est excute dans les cas suivants:
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)
initialisation d'une variable nomme avec une liste entre accolades-clos des expressions ou des listes imbriques (contrevent-init-list)
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)
initialisation d'un temporaire sans nom avec une liste contrevents-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)
initialisation d'un objet avec la dure de stockage dynamique avec une nouvelle expression, o l'initialiseur est une orthse-init-list
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)
dans un communiqu return avec contrevent-init-list utilise comme expression de retour
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)
dans une expression d'appel de fonction, avec embase-init-list utilise comme un argument
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)
dans une expression d'indice avec un oprateur dfini par l'utilisateur []
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)
dans une expression de cast fonctionnelle ou d'une autre initialisation directe, avec embase-init-list utilise comme argument du constructeur
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)
dans un initialiseur non-membre de donnes statiques
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)
dans une liste d'initialisation du constructeur
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)
sur la droite du ct du signe gal (semblable la copie d'initialisation)
Original:
on the right-hand-side of the equals sign (similar to la copie d'initialisation)
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 de la liste d'un objet de T type sont:
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.
  • Si la liste contrevent-init-est vide et T est un type classe avec un constructeur par dfaut, la valeur d'initialisation est effectue .
    Original:
    If the braced-init-list is empty and T is a class type with a default constructor, la valeur d'initialisation is performed.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Sinon, si T est une spcialisation de std::initializer_list, un objet std::initializer_list nouvelle du mme type est construite et utilise pour diriger-initialiser ou copier-initialiser l'objet de T type, selon le contexte .
    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.
  • Dans le cas contraire, les constuctors de T sont considres, en deux phases:
    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.
  • Tous les constructeurs qui prennent std::initializer_list comme seul argument, ou comme le premier argument si les arguments restants ont des valeurs par dfaut, sont examins, et reconnue par la rsolution de surcharge contre un seul argument de std::initializer_list type
    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.
  • Si l'tape prcdente ne produit pas un match, tous les constructeurs de T participer la rsolution de surcharge contre l'ensemble des arguments qui se compose des lments de la contrevent-init-list, avec la restriction que seuls les conversions restrictives sont autoriss. Si cette tape produit un constructeur explicite que la meilleure correspondance pour obtenir une copie de liste d'initialisation, la compilation choue (note, en simple copier-initialisation, constructeurs explicites ne sont pas considrs du tout)
    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.
  • Sinon, si T est le type de rfrence, un prvalue temporaire du type rfrenc est initialise liste, et la rfrence est li celui temporaire .
    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.
  • Dans le cas contraire, si la liste contrevents-init-n'a qu'un seul lment, T est directe initialis ou copie-initialis, selon le contexte, sauf que les conversions restrictives ne sont pas autoriss .
    Original:
    Otherwise, if the braced-init-list has only one element, T is directe initialis or copie-initialis, 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.
  • Dans le cas contraire, si la liste contrevents-init-n'a pas d'lments, T est valeur initialise .
    Original:
    Otherwise, if the braced-init-list has no elements, T is valeur initialise.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Conversions restrictives

liste d'initialisation de la limite permise par les conversions implicites interdire ce qui suit:
Original:
list-initialization limits the allowed les conversions implicites 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.
  • conversion d'un type virgule flottante en un type entier
    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.
  • conversion d'un long double double ou float et la conversion de double float, sauf si la source est une expression constante dont la valeur peut tre stocke exactement le type de cible
    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.
  • conversion du type d'numration entier ou sans porte au type entier qui ne peut pas reprsenter toutes les valeurs de l'original, sauf si la source est une expression constante dont la valeur peut tre stocke exactement le type de cible
    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.

Notes

Braced-init-list n'est pas une expression et n'a pas de type part entire: par exemple, lorsque vous appelez un modle de fonction, contrevents-init-list argument ne peut pas tre utilis pour la dduction type de modle. Une exception est faite pour le auto mot-cl, qui en dduit une embase-init-liste comme 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.

Exemple

#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 << ' ';
}

Rsultat :

world
0 1 1
abcd cd aa
1 a
2 abc
3 abcd
1 2 3 -1 -2 -3


Voir aussi


Web Proxy Viewer  |  New URL  |  Original Page