std::forward
来自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. |
| Defined in header <utility>
|
||
| template< class T > T&& forward( typename std::remove_reference<T>::type& t ); |
(1) | (C++11 起) |
| template< class T > T&& forward( typename std::remove_reference<T>::type&& t ); |
(2) | (C++11 起) |
轉發。
使用時可根據下面的配方在一個函數模板,轉發給另一個函數的參數,正是因為它是傳遞給調用函數.
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)); }
- 如果調用
wrapper()通過一個rvaluestd::string,那麼T推導出std::string(不std::string&,const std::string&,或std::string&&),和std::forward確保一個右值引用傳遞給foo.Original:If a call towrapper()passes an rvaluestd::string, thenTis deduced tostd::string(notstd::string&,const std::string&, orstd::string&&), andstd::forwardensures 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. - 如果調用
wrapper()通過一個常量左值std::string,然後T推導出const std::string&,並std::forward確保一個常量左值的引用被傳遞給foo.Original:If a call towrapper()passes a const lvaluestd::string, thenTis deduced toconst std::string&, andstd::forwardensures 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. - 如果調用
wrapper()通過一個non-const左值std::string,那麼T推導出std::string&,並std::forward確保一個non-const左值的引用被傳遞給foo.Original:If a call towrapper()passes a non-const lvaluestd::string, thenTis deduced tostd::string&, andstd::forwardensures 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.
目錄 |
[编辑] 注釋
嘗試發送一個右值作為左值,如表2)與左值的引用類型T的實例,是一個編譯時錯誤.
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.
[编辑] 參數
| t | - | 該對象被轉發
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. |
[编辑] 返回值
static_cast<T&&>(t)
[编辑] 例外
[编辑] 為例
這個例子演示了完美的參數的功能make_unique()的參數的構造函數的類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) { 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 }
Output:
rvalue overload, n=2 lvalue overload, n=1
[编辑] 複雜性
常數
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.
[编辑] 另請參閱
| (C++11) |
獲得一個右值引用 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. (函數模板) |
| (C++11) |
獲得一個右值引用的舉動構造方法不拋出 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. (函數模板) |