std::forward
Aus 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. |
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.
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 rvaluestd::string
, dannT
wirdstd::string
(nichtstd::string&
,const std::string&
oderstd::string&&
) abgeleitet undstd::forward
sichergestellt, dass ein R-Wert auffoo
übergeben wird .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. - Wenn ein Aufruf an
wrapper()
übergibt einen const lvaluestd::string
, dannT
wirdconst std::string&
abgeleitet undstd::forward
sichergestellt, dass ein const lvalue Verweis auffoo
übergeben wird .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. - Wenn ein Aufruf an
wrapper()
übergibt eine nicht konstante Lvaluestd::string
, dann wirdT
std::string&
abgeleitet undstd::forward
sichergestellt, dass ein nicht-const Lvalue Bezugnahme auffoo
geleitet wird .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.
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.
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
[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.
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.
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) |
(C++11) | 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) |