命名空间
变体
操作

std::forward

来自 cppreference.cn
< cpp‎ | utility
 
 
 
定义于头文件 <utility>
(1)
template< class T >
T&& forward( typename std::remove_reference<T>::type& t ) noexcept;
(since C++11)
(until C++14)
template< class T >
constexpr T&& forward( std::remove_reference_t<T>& t ) noexcept;
(since C++14)
(2)
template< class T >
T&& forward( typename std::remove_reference<T>::type&& t ) noexcept;
(since C++11)
(until C++14)
template< class T >
constexpr T&& forward( std::remove_reference_t<T>&& t ) noexcept;
(since 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() 的调用传递常量左值 std::string,则 T 被推导为 const std::string&,并且 std::forward 确保将常量左值引用传递给 foo
  • 如果对 wrapper() 的调用传递非常量左值 std::string,则 T 被推导为 std::string&,并且 std::forward 确保将非常量左值引用传递给 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)
将参数转换为 xvalue
(函数模板) [编辑]
如果移动构造函数不抛出异常,则将参数转换为 xvalue
(函数模板) [编辑]