名前空間
変種
操作

std::forward

提供: cppreference.com
< cpp‎ | utility

 
 
ユーティリティライブラリ
支援を入力します (basic types, RTTI, type traits)
動的なメモリ管理
エラー処理
プログラムユーティリティ
可変個引数関数
日付と時刻
関数オブジェクト
initializer_list(C++11)
bitset
hash(C++11)
関係演算子
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
ペアとタプル
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
フォワード、スワップ、および移動
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
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.
template<typename T>
wrapper(T&& arg) {
  foo(std::forward<T>(arg));
}


  • wrapper()への呼び出しは右辺値std::stringに合格した場合、その後Tstd::string(ないstd::string&const std::string&、またはstd::string&&)を推定し、std::forwardは右辺値参照が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.
  • wrapper()への呼び出しはconst左辺値std::stringに合格した場合、その後Tconst std::string&に推定し、std::forwardはconst左辺値参照は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.
  • wrapper()への呼び出しは非const左辺値std::stringに合格した場合、その後Tstd::string&に推定し、std::forwardは非const左辺値参照が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.

目次

[編集] ノート

そのような左辺値参照型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.

[編集] パラメータ

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)

[編集] 例外

noexcept specification:  
noexcept
  (C++11およびそれ以降)

[編集]

この例では、クラス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
}

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.

[編集] も参照してください

(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.

(関数テンプレート) [edit]
ムーブコンストラクタがスローされない場合、右辺値参照を取得します
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.

(関数テンプレート) [edit]