close
Espaces de noms
Variantes

std::forward

De cppreference.com

<metanoindex/>

<tbody> </tbody>
Déclaré dans l'en-tête <utility>
template< class T > T&& forward( typename std::remove_reference<T>::type& t );
(1) (depuis C++11)
template< class T > T&& forward( typename std::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.
template<typename T>
wrapper(T&& arg) {
  foo(std::forward<T>(arg));
}


  • Si un appel à wrapper() passe un std::string rvalue, puis T est déduite de std::string (pas std::string&, const std::string& ou std::string&&), et veille à ce que std::forward une référence rvalue est passé à foo .
    Original:
    If a call to wrapper() passes an rvalue std::string, then T is deduced to std::string (not std::string&, const std::string&, or std::string&&), and std::forward ensures that an rvalue reference is passed to foo.
    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 const std::string, puis T est déduite de const std::string&, et veille à ce que std::forward une lvalue const référence est passée à foo .
    Original:
    If a call to wrapper() passes a const lvalue std::string, then T is deduced to const std::string&, and std::forward ensures that a const lvalue reference is passed to foo.
    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 un std::string lvalue non-const, alors T est déduite de std::string&, et veille à ce que std::forward une référence lvalue non-const est passé à foo .
    Original:
    If a call to wrapper() passes a non-const lvalue std::string, then T is deduced to std::string&, and std::forward ensures that a non-const lvalue reference is passed to foo.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

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.

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.

Retourne la valeur

static_cast<T&&>(t)

Exceptions

noexcept specification:  
<tbody> </tbody>
noexcept
   (depuis C++11)

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.

#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)
{
    return std::unique_ptr<T>(new T(std::forward<U>(u)));
}

int main()
{
    std::unique_ptr<A> p1 = make_unique<A>(2); // rvalue
    int i = 1;
    std::unique_ptr<A> p2 = make_unique<A>(i); // lvalue
}

Résultat :

rvalue overload, n=2
lvalue overload, n=1

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.

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) [edit]
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) [edit]