std::forward

来自cppreference.com

 
 
實用工具庫
類型的支持 (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 起)
轉發。 使用時可根據下面的配方在一個函數模板,轉發給另一個函數的參數,正是因為它是傳遞給調用函數.
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()通過一個rvaluestd::string,那麼T推導出std::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()通過一個常量左值std::string,然後T推導出const std::string&,並std::forward確保一個常量左值的引用被傳遞給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()通過一個non-const左值std::string,那麼T推導出std::string&,並std::forward確保一個non-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.

目錄

[编辑] 注釋

嘗試發送一個右值作為左值,如表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.

[编辑] 參數

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 起)

[编辑] 為例

這個例子演示了完美的參數的功能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.

#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]