Namensräume
Varianten

std::forward

Aus cppreference.com
< cpp‎ | utility

definiert in Header <utility>
template<class T >
T&& forward(typenamestd::remove_reference<T>::type& t );
(1) (seit C++11)
template<class T >
T&& forward(typenamestd::remove_reference<T>::type&& t );
(2) (seit C++11)
Wenn nach dem folgenden Rezept in einer Funktions-Template, leitet das Argument einer anderen Funktion genau wie an die aufrufende Funktion übergeben wurde verwendet .
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));}


  • Wenn ein Aufruf an wrapper() übergibt ein rvalue std::string, dann T wird std::string (nicht std::string&, const std::string& oder std::string&&) abgeleitet und std::forward sichergestellt, dass ein R-Wert auf foo übergeben wird .
    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.
  • Wenn ein Aufruf an wrapper() übergibt einen const lvalue std::string, dann T wird const std::string& abgeleitet und std::forward sichergestellt, dass ein const lvalue Verweis auf foo übergeben wird .
    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.
  • Wenn ein Aufruf an wrapper() übergibt eine nicht konstante Lvalue std::string, dann wird Tstd::string& abgeleitet und std::forward sichergestellt, dass ein nicht-const Lvalue Bezugnahme auf foo geleitet wird .
    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.

Inhaltsverzeichnis

[Bearbeiten]Notes

Der Versuch, einen rvalue als Lvalue, wie durch Instanziieren der Form 2) mit lvalue Referenz-Typ T weiterzuleiten, ist ein Compiler-Fehler .
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.

[Bearbeiten]Parameter

t -
das Objekt weitergeleitet werden soll
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.

[Bearbeiten]Rückgabewert

static_cast<T&&>(t)

[Bearbeiten]Ausnahmen

noexcept specification:  
noexcept
  (seit C++11)

[Bearbeiten]Beispiel

Dieses Beispiel demonstriert perfekte Weiterleitung der Parameter der Funktion make_unique (), um das Argument des Konstruktors der Klasse 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){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}

Output:

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

[Bearbeiten]Komplexität

Constant
Original:
Constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten]Siehe auch

(C++11)
erhält einen rvalue Referenz
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.

(Funktions-Template)[edit]
erhält eine rvalue Referenz, wenn der Umzug Konstruktor nicht werfen wird
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.

(Funktions-Template)[edit]
close