Namespaces
Variants

std:: forward

From cppreference.net
Utilities library
定义于头文件 <utility>
(1)
template < class T >
T && forward ( typename std:: remove_reference < T > :: type & t ) noexcept ;
(C++11 起)
(C++14 前)
template < class T >
constexpr T && forward ( std:: remove_reference_t < T > & t ) noexcept ;
(C++14 起)
(2)
template < class T >
T && forward ( typename std:: remove_reference < T > :: type && t ) noexcept ;
(C++11 起)
(C++14 前)
template < class T >
constexpr T && forward ( std:: remove_reference_t < T > && t ) noexcept ;
(C++14 起)
1) 根据T的类型,将左值转发为左值或右值。

t 转发引用 (声明为 cv-未限定函数模板参数的右值引用的函数参数)时,此重载将参数转发给另一个函数,并保持其传递给调用函数时的 值类别

例如,若在如下包装器中使用时,该模板的行为如下所述:

template<class T>
void wrapper(T&& arg)
{
    // arg 始终是左值
    foo(std::forward<T>(arg)); // 根据 T 的类型决定转发为左值或右值
}
  • 如果对 wrapper() 的调用传递了一个右值 std::string ,则 T 被推导为 std::string (而非 std::string& const std::string& std::string&& ),且 std::forward 确保向 foo 传递右值引用。
  • 如果对 wrapper() 的调用传递了一个常量左值 std::string ,则 T 被推导为 const std::string& ,且 std::forward 确保向 foo 传递常量左值引用。
  • 如果对 wrapper() 的调用传递了一个非常量左值 std::string ,则 T 被推导为 std::string& ,且 std::forward 确保向 foo 传递非常量左值引用。
2) 将右值作为右值转发,并禁止将右值作为左值转发。

此重载使得能够将表达式结果(例如函数调用)作为转发引用参数的原始值类别进行转发,该结果可能是右值或左值。

例如,如果一个包装器不仅转发其参数,而是调用参数上的成员函数,并转发其结果:

// 转换包装器
template<class T>
void wrapper(T&& arg)
{
    foo(forward<decltype(forward<T>(arg).get())>(forward<T>(arg).get()));
}

其中 arg 的类型可能是

struct Arg
{
    int i = 1;
    int  get() && { return i; } // 对该重载的调用是右值
    int& get() &  { return i; } // 对该重载的调用是左值
};

尝试将右值作为左值转发,例如用左值引用类型 T 实例化形式 (2) ,会导致编译时错误。

目录

注释

请参阅 模板实参推导 了解转发引用(函数参数中使用的 T&& )背后的特殊规则,以及 转发引用 获取其他详细信息。

参数

t - 待转发的对象

返回值

static_cast < T && > ( t )

复杂度

常量。

示例

本示例演示了如何将参数完美转发给类 T 构造函数的实参。同时展示了参数包的完美转发。

#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'; }
};
class B
{
public:
    template<class T1, class T2, class T3>
    B(T1&& t1, T2&& t2, T3&& t3) :
        a1_{std::forward<T1>(t1)},
        a2_{std::forward<T2>(t2)},
        a3_{std::forward<T3>(t3)}
    {}
private:
    A a1_, a2_, a3_;
};
template<class T, class U>
std::unique_ptr<T> make_unique1(U&& u)
{
    return std::unique_ptr<T>(new T(std::forward<U>(u)));
}
template<class T, class... U>
std::unique_ptr<T> make_unique2(U&&... u)
{
    return std::unique_ptr<T>(new T(std::forward<U>(u)...));
}
auto make_B(auto&&... args) // since C++20
{
    return B(std::forward<decltype(args)>(args)...);
}
int main()
{
    auto p1 = make_unique1<A>(2); // rvalue
    int i = 1;
    auto p2 = make_unique1<A>(i); // lvalue
    std::cout << "B\n";
    auto t = make_unique2<B>(2, i, 3);
    std::cout << "make_B\n";
    [[maybe_unused]] B b = make_B(4, i, 5);
}

输出:

rvalue overload, n=2
lvalue overload, n=1
B
rvalue overload, n=2
lvalue overload, n=1
rvalue overload, n=3
make_B
rvalue overload, n=4
lvalue overload, n=1
rvalue overload, n=5

参见

(C++11)
将实参转换为亡值
(函数模板)
若移动构造函数不抛出异常则将实参转换为亡值
(函数模板)