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

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

copy 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 d'un autre objet
Original:
Initializes an object from another object
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 = other ; (1)
f(other); (2)
return other; (3)
catch ( T other) ; (4)
T array [ N ] = { other }; (5)

Explication

Initialisation de la copie est effectue dans les cas suivants:
Original:
Copy 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)
quand une variable nomme (automatique, statique ou thread-local) est dclare avec l'initialiseur constitue d'un signe gal suivi d'une expression .
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
en passant un argument une fonction par valeur
Original:
when passing an argument to a function by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
au retour d'une fonction qui retourne la valeur
Original:
when returning from a function that returns by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
lors de la capture d'une exception en valeur
Original:
when catching an exception by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
dans le cadre de initialisation d'agrgats, pour initialiser chaque lment pour lequel il est prvu une initialisation
Original:
as part of initialisation d'agrgats, to initialize each element for which an initializer is provided
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 copie sont:
Original:
The effects of copy initialization are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Si T est un type de classe et le type de other est cv-inconditionnel de la version T ou une classe drive de T, les constructeurs de T sont examines et la meilleure correspondance est choisi par la rsolution de surcharge. Le constructeur est alors appele pour initialiser l'objet .
    Original:
    If T is a class type and the type of other is cv-unqualified version of T or a class derived from T, the constructors of T 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.
  • Si T est un type de classe et le type de other est diffrent, ou si T n'est pas de type classe, mais le type de other est un type de classe, des squences dfinies par l'utilisateur de conversion qui peut convertir le type de other T sont examins et le meilleur on est slectionn grce la rsolution de surcharge. Le rsultat de la conversion, qui est une prvalue temporaire du type de destination, est ensuite utilis pour diriger-initialiser l'objet. La dernire tape est gnralement limin et le rsultat de la fonction de conversion est construit directement dans la mmoire alloue pour l'objet cible, mais le constructeur de copie est ncessaire pour tre accessible mme si elle n'est pas utilise .
    Original:
    If T is a class type, and the type of other is different, or if T is non-class type, but the type of other is a class type, des squences dfinies par l'utilisateur de conversion that can convert from the type of other to T are examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary of the destination type, is then used to diriger-initialiser the object. The last step is usually limin and the result of the conversion function is constructed directly in the memory allocated for the target object, but the copy constructor is required to be accessible even though it's not used.
    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 aucun T ni le type de other sont les types de classes), conversions standard sont utiliss, si ncessaire, pour convertir la valeur de other la version cv-inconditionnel de T .
    Original:
    Otherwise (if neither T nor the type of other are class types), conversions standard are used, if necessary, to convert the value of other to the cv-unqualified version of T.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Notes

Copier-initialisation est moins permissive que l'initialisation directe: la copie d'initialisation ne tient compte que non explicites constructeurs et les fonctions de conversion dfinis par l'utilisateur .
Original:
Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Si other est une expression rvalue, dplacer constructeur seront slectionns par la rsolution de surcharge et a appel au cours de la copie d'initialisation .
Original:
If other is an rvalue expression, dplacer constructeur will be selected by overload resolution and called during copy-initialization.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Le signe gal, =, dans la copie d'initialisation d'une variable nomme n'est pas li l'oprateur d'affectation. Les surcharges d'oprateur d'affectation n'ont aucun effet sur la copie d'initialisation .
Original:
The equals sign, =, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.
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 <utility>
#include <memory>

int main()
{
    std::string s = "test"; // OK: constructor is non-explicit
    std::string s2 = std::move(s); // this copy-initialization performs a move

//  std::unique_ptr<int> p = new int(1); // error: constructor is explicit
    std::unique_ptr<int> p(new int(1)); // OK: direct-initialization

    int n = 3.14;    // floating-integral conversion
    const int b = n; // const doesn't matter
    int c = b;       // ...either way
}


Voir aussi


Web Proxy Viewer  |  New URL  |  Original Page