direct initialization
De cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Initialise un objet d'ensemble explicite de arguments du constructeur .
Original:
Initializes an object from explicit set of constructor arguments.
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.
Sommaire |
[modifier] Syntaxe
T object ( arg );
T object |
(1) | ||||||||
T object { arg };
T object |
(2) | (depuis C++11) | |||||||
T ( other )
T |
(3) | ||||||||
static_cast< T >( other )
|
(4) | ||||||||
new T( args, ...)
|
(5) | ||||||||
Class:: Class() : member( args, ...) {...
|
(6) | ||||||||
[ arg](){...
|
(7) | (depuis C++11) | |||||||
[modifier] Explication
L'initialisation directe est effectuée dans les cas suivants:
Original:
Direct 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)
initialisation d'une liste entre parenthèses d'expressions non vide
Original:
initialization with a nonempty parenthesized list of expressions
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)
au cours de la séquence liste d'initialisation, si aucun constuctors initialiseur de liste sont prévus et un constructeur correspondant est accessible, et toutes les conversions nécessaires implicites sont non-rétrécissement .
Original:
during liste d'initialisation sequence, if no initializer-list constuctors are provided and a matching constructor is accessible, and all necessary implicit conversions are non-narrowing.
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)
initialisation d'un prvalue temporaire par fonte fonctionnelle ou avec une liste d'expressions entre parenthèses
Original:
initialization of a prvalue temporary by fonte fonctionnelle or with a parenthesized expression 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)
initialisation d'un prvalue temporaire par un expession static_cast
Original:
initialization of a prvalue temporary by a static_cast expession
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)
l'initialisation d'un objet avec la durée de stockage dynamique d'un nouveau-expression d'un initialiseur non vide
Original:
initialization of an object with dynamic storage duration by a new-expression with a non-empty 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.
6)
l'initialisation d'une base ou d'un élément non-statique en liste d'initialisation constructeur
Original:
initialization of a base or a non-static member by constructor 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.
You can help to correct and verify the translation. Click here for instructions.
7)
initialisation des membres de l'objet de fermeture à partir des variables capturées par copie dans une lambda-expression
Original:
initialization of closure object members from the variables caught by copy in a lambda-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.
Les effets de l'initialisation directe sont les suivants:
Original:
The effects of direct initialization 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.
- Si
T
est un type de classe, les constructeurs deT
sont examinés et la meilleure correspondance est sélectionné par la résolution de surcharge. Le constructeur est alors appelée pour initialiser l'objet .Original:IfT
is a class type, the constructors ofT
are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Autrement, si
T
est un type non-classe, conversions standard sont utilisés, si nécessaire, à convertir la valeur de other à la version CV-inconditionnel deT
.Original:Otherwise, ifT
is a non-class type, conversions standard are used, if necessary, to convert the value of other to the cv-unqualified version ofT
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifier] Notes
Direct-initialisation est plus permissif que la copie d'initialisation: la copie d'initialisation ne tient compte que non explicites constructeurs et des fonctions UDF de conversion, tandis que l'initialisation directe considère tous les constructeurs et les séquences de conversion implicites .
Original:
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions, while direct-initialization considers all constructors and implicit conversion sequences.
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.
[modifier] Exemple
#include <string> #include <iostream> #include <memory> struct Foo { int mem; explicit Foo(int n) : mem(n) {} }; int main() { std::string s1("test"); // constructor from const char* std::string s2(10, 'a'); std::unique_ptr<int> p(new int(1)); // OK: explicit constructors allowed // std::unique_ptr<int> p = new int(1); // error: constructor is explicit Foo f(2); // f is direct-initialized: // constructor parameter n is copy-initialized from the rvalue 2 // f.mem is direct-initialized from the parameter n // Foo f2 = 2; // error: constructor is explicit std::cout << s1 << ' ' << s2 << ' ' << *p << ' ' << f.mem << '\n'; }
Résultat :
test aaaaaaaaaa 1 2