【C++编程】std::forward与完美转发

std::forward 完美转发

 1. std::forward代码原型:

template <class T>
T&& forward(typename remove_reference<T>::type& t) noexcept {
    return static_cast<T&&>(t);
}

template <class T>
T&& forward(typename remove_reference<T>::type&& t) noexcept {
    return static_cast<T&&>(t);
}

2. 分析std::forward<T>实现条件转发的原理(以转发Widget类对象为例

 

 

当传递给func函数的实参类型为左值Widget时,T被推导为Widget &,然后forward会实例化为std::forward<Widget &>,并返回Widget&(左值引用,根据定义是个左值!)而当传递给func函数的实参类型为右值Widget时,T被推导为Widget。然后forward被实例化为std::forward<Widget>,并返回Widget&&(注意,匿名的右值引用是个右值!)

可见,std::forward会根据传递给func函数实参(注意,不是形参)的左/右值类型进行转发。当传给func函数左值实参时,forward返回左值引用,并将该左值转发给process。而当传入func的实参为右值时,forward返回右值引用,并将该右值转发给process函数。

 1 #include <iostream>
 2 using namespace std;
 3 
 4 template <typename Tp>
 5 inline typename std::remove_reference<_Tp>::type &&my_move(Tp &&t)
 6 {
 7   cout << &t << endl;
 8   return static_cast<typename std::remove_reference<Tp>::type &&>(t);
 9 }
10 
11 template <typename Tp>
12 inline Tp &&my_forward(typename std::remove_reference<Tp>::type &t)
13 {
14   cout << "calling lvalue forward" << endl;
15   return static_cast<Tp &&>(t);
16 }
17 
18 template <typename Tp>
19 inline Tp &&my_forward(typename std::remove_reference<Tp>::type &&t)
20 {
21   cout << "calling rvalue forward" << endl;
22   static_assert(!std::is_lvalue_reference<_Tp>::value,
23                 "template argument substituting Tp is an lvalue reference type");
24   return static_cast<Tp &&>(t);
25 }
26 
27 struct X
28 {
29 };
30 
31 void inner(const X &) { cout << "inner(const X&)" << endl; }
32 void inner(X &&) { cout << "inner(X&&)" << endl; }
33 template <typename T>
34 void outer(T &&t)
35 {
36   inner(my_forward<T>(t));
37 }
38 
39 int main()
40 {
41   cout << ">>> test move" << endl;
42   X a1;
43   cout << &a1 << endl;
44   cout << "> test lvalue" << endl;
45   X &&b = my_move(a1);
46   cout << "> test rvalue" << endl;
47   X &&c = my_move(X());
48 
49   cout << endl;
50 
51   cout << ">>> test forward" << endl;
52   X a2;
53   cout << "> test lvalue indirectly" << endl;
54   outer(a2);
55   cout << "> test rvalue indirectly" << endl;
56   outer(X());
57   cout << "> test rvalue directly" << endl;
58   inner(my_forward<X>(X()));
59 

须知,std::movestd::forward都是仅仅执行强制型别转换的函数,std::move 无条件的将实参强制转换成右值, 而std::forward 则 仅在某个特定的条件满足时才执行同一类型转换。

 

示例:

#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

参考资料

1. 第15课 完美转发(std::forward)

2. C++11 std::move和std::forward

3. C++11:std::move和std::forward源码分析

posted @ 2020-09-06 20:48  苏格拉底的落泪  阅读(514)  评论(0编辑  收藏  举报