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

std::forward cppreference.com
cppreference.com
Espaces de noms
Variantes

std::forward

De cppreference.com

<metanoindex/>

<tbody> </tbody>
Dclar dans l'en-tte <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 modle 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 dduite de std::string (pas std::string&, const std::string& ou std::string&&), et veille ce que std::forward une rfrence 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 dduite de const std::string&, et veille ce que std::forward une lvalue const rfrence est passe 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 dduite de std::string&, et veille ce que std::forward une rfrence 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 rfrence 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.

Paramtres

t -
l'objet transfrer
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 paramtre 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
}

Rsultat :

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 rfrence 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 gnrique) [edit]
obtient une rfrence 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 gnrique) [edit]

Web Proxy Viewer  |  New URL  |  Original Page