reference 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. |
Lié une référence à un objet
Original:
Binds a reference to an object
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 & ref = object ;
T T |
(1) | ||||||||
T && ref = object ;
T T |
(2) | (depuis C++11) | |||||||
R fn ( T & arg );
or R fn fn |
(3) | ||||||||
T & fn () {
or T
|
(4) | ||||||||
[modifier] Explication
Une référence à
T
peut être initialisé avec un objet de T
type, en fonction de T
type, ou d'un objet implicitement convertible en T
. Une fois initialisé, une référence ne peut pas être modifié pour faire référence à un autre objet .Original:
A reference to
T
can be initialized with an object of type T
, a function of type T
, or an object implicitly convertible to T
. Once initialized, a reference cannot be changed to refer to another object.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 références sont initialisés dans les situations suivantes:
Original:
References are initialized 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)
Quand un nom de référence lvalue variable est déclarée avec un initialiseur
Original:
When a named lvalue reference variable is declared with an 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.
2)
Quand une variable de référence rvalue nommée est déclarée avec un initialiseur
Original:
When a named rvalue reference variable is declared with an 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.
3)
Dans une expression d'appel de fonction, lorsque le paramètre de fonction est du type de référence
Original:
In a function call expression, when the function parameter has reference type
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)
Dans la déclaration return, lorsque la fonction retourne un type référence
Original:
In the return statement, when the function returns a reference type
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 de référence sont les suivants:
Original:
The effects of reference 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 la référence est une référence lvalue:Original:If the reference is an lvalue reference:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Si object est une expression lvalue, et son type est
T
ou une base deT
, et est également cv ou moins qualifiés, alors la référence est lié à l'objet identifié par la lvalue ou le sous-objet de classe de base de l'objet .Original:If object is an lvalue expression, and its type isT
or a base ofT
, and is equally or less cv-qualified, then the reference is bound to the object identified by the lvalue or the base class subobject of 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 object est une expression lvalue, et son type est implicitement convertible en un type qui est soit
T
ou une base deT
, égale ou moindre cv-qualifié, alors les fonctions de conversion non explicites du type de source et de ses classes de base qui retourne références lvalue sont envisagées et la meilleure solution est choisie par la résolution de surcharge. La référence est alors lié à l'objet identifié par la lvalue retourné par la fonction de conversion (ou son sous-objet de classe de base)Original:If object is an lvalue expression, and its type is implicitly convertible to a type that is eitherT
or a base ofT
, equally or less cv-qualified, then the non-explicit conversion functions of the source type and its base classes that return lvalue references are considered and the best one is selected by overload resolution. The reference is then bound to the object identified by the lvalue returned by the conversion function (or its base class subobject)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 référence est soit rvalue de référence ou une référence lvalue à const:Original:Otherwise, if the reference is either rvalue reference or lvalue reference to const:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Si object est un xValue, un prvalue classe, un tableau prvalue, ou un type de fonction qui est soit lvalue
T
ou une base deT
, égale ou inférieure cv-qualifié, le renvoi est lié à la valeur de l'expression d'initialisation ou de son sous-objet de base .Original:If object is an xvalue, a class prvalue, an array prvalue, or a function lvalue type that is eitherT
or a base ofT
, equally or less cv-qualified, then the reference is bound to the value of the initializer expression or its base subobject.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- Si object est une expression de type classe qui peut être implicitement converti en un xValue, un prvalue classe ou d'une valeur de fonction de type qui est soit
T
ou une base deT
, égale ou moindre cv-qualifié, alors la référence est lié à la résultat de la conversion ou de son sous-objet de base .Original:If object is a class type expression that can be implicitly converted to an xvalue, a class prvalue, or a function value of type that is eitherT
or a base ofT
, equally or less cv-qualified, then the reference is bound to the result of the conversion or its base subobject.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, un temporaire du
T
type est construit et copie-initialisé de object. La référence est alors lié à cette temporaire .Original:Otherwise, a temporary of typeT
is constructed and copie-initialisé from object. The reference is then bound to this temporary.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
[modifier] Durée de vie d'un fichier temporaire
Chaque fois qu'une référence est lié à un emploi temporaire ou à un sous-objet de base de manière temporaire, la durée de vie de l'application temporaire est prolongée en fonction de la durée de vie de la référence, avec les exceptions suivantes:
Original:
Whenever a reference is bound to a temporary or to a base subobject of a temporary, the lifetime of the temporary is extended to match the lifetime of the reference, with the following exceptions:
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.
- un temporaire liée à une valeur de retour d'une fonction dans un état return n'est pas étendu: il est détruit immédiatement à la fin de l'expression de retour. Une telle fonction renvoie toujours une référence pendantes .Original:a temporary bound to a return value of a function in a return statement is not extended: it is destroyed immediately at the end of the return expression. Such function always returns a dangling reference.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - temporaire lié à un élément de référence dans un liste d'initialisation constructeur ne persiste que jusqu'à ce que les sorties de constructeur, pas tant que l'objet existe .Original:a temporary bound to a reference member in a constructor liste d'initialisation persists only until the constructor exits, not as long as the object exists.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - temporaire lié à un paramètre de référence dans un appel de fonction existe jusqu'à la fin de la pleine expression contenant cet appel de fonction: si la fonction retourne une référence, qui survit à la pleine expression, il devient une référence pendantes .Original:a temporary bound to a reference parameter in a function call exists until the end of the full expression containing that function call: if the function returns a reference, which outlives the full expression, it becomes a dangling reference.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - temporaire liée à une référence dans l'initialiseur utilisé dans une nouvelle expression existe jusqu'à la fin de la pleine expression contenant cette nouvelle expression, pas tant que l'objet initialisé. Si l'objet initialisé survit à la pleine expression, son élément de référence devient une référence pendantes .Original:a temporary bound to a reference in the initializer used in a new-expression exists until the end of the full expression containing that new-expression, not as long as the initialized object. If the initialized object outlives the full expression, its reference member becomes a dangling reference.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
En général, la durée de vie d'un temporaire ne peut pas être étendue d'"le transmettre»: une deuxième référence, initialisée à partir de la référence à laquelle le temporaire est tenu, n'affecte pas sa durée de vie .
Original:
In general, the lifetime of a temporary cannot be further extended by "passing it on": a second reference, initialized from the reference to which the temporary was bound, does not affect its lifetime.
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] Notes
Références apparaître sans que dans la déclaration initialiseurs paramètre de la fonction, dans la déclaration de type de fonction de retour, dans la déclaration d'un membre du groupe, et avec le spécificateur
extern
.Original:
References appear without initializers only in function parameter declaration, in function return type declaration, in the declaration of a class member, and with the
extern
specifier.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 <utility> #include <sstream> struct S { int mi; const std::pair<int,int>& mp; // reference member }; void foo(int) {} struct A {}; struct B : A { int n; operator int&() { return n; }; }; B bar() {return B(); } //int& bad_r; // error: no initializer extern int& ext_r; // OK int main() { // lvalues int n = 1; int& r1 = n; // lvalue reference to the object n const int& cr(n); // reference can be more cv-qualified volatile int& cv{n}; // any initializer syntax can be used int& r2 = r1; // another lvalue reference to the object n // int& bad = cr; // error: less cv-qualified int& r3 = const_cast<int&>(cr); // const_cast is needed void (&rf)(int) = foo; // lvalue reference to function int ar[3]; int (&ra)[3] = ar; // lvalue reference to array B b; A& base_ref = b; // reference to base subobject int& converted_ref = b; // reference to the result of a conversion // rvalues // int& bad = 1; // error: cannot bind lvalue ref to rvalue const int& cref = 1; // bound to rvalue int&& rref = 1; // bound to rvalue const A& cref2 = bar(); // reference to A subobject of B temporary A&& rref2 = bar(); // same int&& xref = static_cast<int&&>(n); // bind directly to n // int&& copy_ref = n; // error: can't bind to an lvalue double&& copy_ref = n; // bind to an rvalue temporary with value 1.0 // restrictions on temporary lifetimes std::ostream& buf_ref = std::ostringstream() << 'a'; // the ostringstream temporary // was bound to the left operand of operator<<, but its lifetime // ended at the semicolon: buf_ref is now a dangling reference. S a { 1, {2,3} }; // temporary pair {2,3} bound to the reference member // a.mp and its lifetime is extended to match a S* p = new S{ 1, {2,3} }; // temporary pair {2,3} bound to the reference // member a.mp, but its lifetime ended at the semicolon // p->mp is a dangling reference delete p; }