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以来) |
の引数に関数make_unique()のパラメータの完全な転送を示しています。
関数テンプレートで以下のレシピ、それは、呼び出し元の関数に渡されたとおりに正確に別の関数の引数に応じて転送するために使用する場合.
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()への呼び出しは右辺値std::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()への呼び出しはconst左辺値std::stringに合格した場合、その後Tはconst std::string&に推定し、std::forwardはconst左辺値参照は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()への呼び出しは非const左辺値std::stringに合格した場合、その後Tはstd::string&に推定し、std::forwardは非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.
目次 |
[編集] ノート
そのような左辺値参照型Tでフォーム2)をインスタンス化することなどにより、左辺値として右辺値を転送しようとすると、コンパイルエラーになり.
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)
[編集] 例外
[編集] 例
この例では、クラス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. (関数テンプレート) |