std::forward
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. |
Déclaré dans l'en-tête <utility> | ||
template<class T > T&& forward(typenamestd::remove_reference<T>::type& t ); | (1) | (depuis C++11) |
template<class T > T&& forward(typenamestd::remove_reference<T>::type&& t ); | (2) | (depuis C++11) |
Lorsqu'il est utilisé selon la recette suivante dans un modèle de fonction, avant l'argument d'une autre fonction exactement comme il a été passé à la fonction d'appel .
Original:
When used according to the following recipe in a function template, forwards the argument to another function exactly as it was passed to the calling function.
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.
template<typename T> wrapper(T&& arg){ foo(std::forward<T>(arg));}
- Si un appel à
wrapper()
passe unstd::string
rvalue, puisT
est déduite destd::string
(passtd::string&
,const std::string&
oustd::string&&
), et veille à ce questd::forward
une référence rvalue est passé àfoo
.Original:If a call towrapper()
passes an rvaluestd::string
, thenT
is deduced tostd::string
(notstd::string&
,const std::string&
, orstd::string&&
), andstd::forward
ensures that an rvalue reference is passed tofoo
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Si un appel à
wrapper()
passe une lvalue conststd::string
, puisT
est déduite deconst std::string&
, et veille à ce questd::forward
une lvalue const référence est passée àfoo
.Original:If a call towrapper()
passes a const lvaluestd::string
, thenT
is deduced toconst std::string&
, andstd::forward
ensures that a const lvalue reference is passed tofoo
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Si un appel à
wrapper()
passe unstd::string
lvalue non-const, alorsT
est déduite destd::string&
, et veille à ce questd::forward
une référence lvalue non-const est passé àfoo
.Original:If a call towrapper()
passes a non-const lvaluestd::string
, thenT
is deduced tostd::string&
, andstd::forward
ensures that a non-const lvalue reference is passed tofoo
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Sommaire |
[modifier]Notes
Tenter de transmettre une rvalue comme un lvalue, comme en instanciant la forme 2) avec référence lvalue de type T, est une erreur de compilation .
Original:
Attempting to forward an rvalue as an lvalue, such as by instantiating the form 2) with lvalue reference type T, is a compile-time error.
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]Paramètres
t | - | l'objet à transférer Original: the object to be forwarded The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[modifier]Retourne la valeur
static_cast<T&&>(t)
[modifier]Exceptions
[modifier]Exemple
Cet exemple illustre le transfert parfait du paramètre de la fonction make_unique () pour l'argument du constructeur de la classe T
Original:
This example demonstrates perfect forwarding of the parameter of the function make_unique() to the argument of the constructor of class T
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.
#include <iostream>#include <memory>#include <utility> struct A { A(int&& n){std::cout<<"rvalue overload, n="<< n <<"\n";} A(int& n){std::cout<<"lvalue overload, n="<< n <<"\n";}}; template<class T, class U>std::unique_ptr<T> make_unique(U&& u){returnstd::unique_ptr<T>(new T(std::forward<U>(u)));} int main(){std::unique_ptr<A> p1 = make_unique<A>(2);// rvalueint i =1;std::unique_ptr<A> p2 = make_unique<A>(i);// lvalue}
Résultat :
rvalue overload, n=2 lvalue overload, n=1
[modifier]Complexité
Constante
Original:
Constant
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]Voir aussi
(C++11) | obtient une référence rvalue Original: obtains an rvalue reference The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) |
(C++11) | obtient une référence rvalue si le constructeur mouvement ne jette pas Original: obtains an rvalue reference if the move constructor does not throw The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (fonction générique) |