命名空间
变体
操作

std::forward

来自 cppreference.cn
< cpp‎ | 工具
 
 
 
在头文件 <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-unqualified 函数模板参数的右值引用形式的函数参数)时,此重载将参数以其传入调用函数时的值类别转发给另一个函数。

例如,如果在一个像下面这样的包装器中使用,模板的行为如下所述:

template<class T>
void wrapper(T&& arg)
{
    // arg is always lvalue
    foo(std::forward<T>(arg)); // Forward as lvalue or as rvalue, depending on T
}
  • 如果对 wrapper() 的调用传入一个右值 std::string,那么 T 被推导为 std::string(而不是 std::string&const std::string&std::string&&),并且 std::forward 确保将一个右值引用传递给 foo
  • 如果对 wrapper() 的调用传入一个 const 左值 std::string,那么 T 被推导为 const std::string&,并且 std::forward 确保将一个 const 左值引用传递给 foo
  • 如果对 wrapper() 的调用传入一个非 const 左值 std::string,那么 T 被推导为 std::string&,并且 std::forward 确保将一个非 const 左值引用传递给 foo
2) 将右值转发为右值,并禁止将右值转发为左值。

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

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

// transforming wrapper
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; } // call to this overload is rvalue
    int& get() &  { return i; } // call to this overload is lvalue
};

尝试将右值转发为左值,例如通过使用左值引用类型 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)
将参数转换为亡值
(函数模板) [编辑]
如果移动构造函数不抛出异常,则将参数转换为亡值
(函数模板) [编辑]