0
点赞
收藏
分享

微信扫一扫

C++11 move和forward实现原理


文章目录

  • ​​一、move与forward的作用​​
  • ​​二、move原理​​
  • ​​三、forward原理​​
  • ​​四、例子​​

一、move与forward的作用

  • move:不能移动任何东西,它唯一的功能是将一个左值强制转化为右值引用,继而可以通过右值引用使用该值,以用于移动语义,从实现上讲,std::move基本等同于一个类型转换:static_cast<T&&>(lvalue)
  • forward: 不转发任何东西,也是执行左值到右值的强制类型转换,只是仅在特定条件满足时才执行该转换
    典型使用场景:某个函数模板取用了万能引用类型为形参,随后把它传递给了另一个函数

move与forward实现均用到了remove_reference,它的作用就是把一个模板类型T中可能蕴含的&号(0个,一个或者两个)给去掉,留下原始不带引用的类型

remove_reference源码如下:

// STRUCT TEMPLATE remove_reference
template <class _Ty>
struct remove_reference {
using type = _Ty;
using _Const_thru_ref_type = const _Ty;
};

template <class _Ty>
struct remove_reference<_Ty&> {
using type = _Ty;
using _Const_thru_ref_type = const _Ty&;
};

template <class _Ty>
struct remove_reference<_Ty&&> {
using type = _Ty;
using _Const_thru_ref_type = const _Ty&&;
};

template <class _Ty>
using remove_reference_t = typename remove_reference<_Ty>::type;

remove_reference针对不同类型(T、T&、T&&),分别重载,只返回不带引用符号的值类型 T

二、move原理

// FUNCTION TEMPLATE move
template <class _Ty>
// forward _Arg as movable
_NODISCARD constexpr remove_reference_t<_Ty>&& move(_Ty&& _Arg) noexcept {
return static_cast<remove_reference_t<_Ty>&&>(_Arg);
}

move就是把不论左值右值都强转为右值,也就是T&&这种类型

这时候就发现其实move也没干什么大事,参数是万能引用类型&&,因此经过引用折叠,调用处传入左值,则_Arg是左值引用T&,调用处传入右值,则_Arg是右值引用T&&

使用 remove_reference_t 获取本来的不带引用的值类型,然后static_cast为对应右值引用类型

三、forward原理

如果说我们想使用引用折叠,形参写为​​T&& val​​,无论传入的实参是左值还是右值,由于形参val是一个有名变量,所以val是左值。如果我们还想通过形参val继续传递实参原本的左值或右值类型,我们需要使用forward进行类型的完美转发

典型使用场景如下:

template<typename T>
void process(const T& lval); // 传入的是左值,process以左值处理
template<typename T>
void process(T&& rval); // 传入的是右值,process以右值处理

template<typename T>
void fun(T&& param){
process(std::forward<T> param);
}

形参param被传递给函数process,而process依据形参是左值还是右值进行了重载,实现不同的操作
我们期望,当调用fun时,传入的实参是左值,则调用左值引用参数的process,传入的实参是右值,则调用右值引用参数的process

forward就可以还原传入实参的左值或右值类型,源码如下:

// FUNCTION TEMPLATE forward
template <class _Ty>
// forward an lvalue as either an lvalue or an rvalue
_NODISCARD constexpr _Ty&& forward(remove_reference_t<_Ty>& _Arg) noexcept {
return static_cast<_Ty&&>(_Arg);
}

template <class _Ty>
// forward an rvalue as an rvalue
_NODISCARD constexpr _Ty&& forward(remove_reference_t<_Ty>&& _Arg) noexcept {
static_assert(!is_lvalue_reference_v<_Ty>, "bad forward call");
return static_cast<_Ty&&>(_Arg);
}

forward提供两个重载版本, 当传入forward的参数为左值时,使用第一个重载版本,当传入forward的参数为右值时,使用第二个重载版本

int val = 7;
fun(val);
fun(47);

对于fun(val),传入的是一个左值, 那么fun中T的类型将是int&,param类型是int& &&,经过折叠为int&。因此,在std::forward模板函数中,推断出_Ty的类型为int&,std::remove_reference用int& 进行实例化,std::remove_reference的type成员是int,在forward源码中有static_cast<int& &&>,则forward会返回左值引用类型

对于fun(7),传入的是一个右值, 那么fun中T的类型将是int,param类型是int&&。因此,在std::forward模板函数中,推断出_Ty的类型为int&&,std::remove_reference用int&& 进行实例化,std::remove_reference的type成员是int,在forward源码中有static_cast<int&& &&>,则forward会返回右值引用类型

这样就能实现,传给形参param的是左值,forward返回左值引用类型;传给形参param的是右值,forward返回右值引用类型

四、例子

template<typename T>
void fun(const T& val) {
cout << "l" << endl;
}

template<typename T>
void fun(T&& val) {
cout << "r" << endl;
}

template<typename T>
void test(T&& param) {
fun(forward<T>(param));
}

int main() {
int a = 1;
test(a);
test(1);
return 0;
}

C++11 move和forward实现原理_开发语言


对于test(a),传入左值,forward返回左值引用类型,然而第一个fun需要const T&,无法匹配,而第二个的参数是T&&,是万能引用类型,所以会匹配第二个重载版本

对于test(1),传入右值,forward返回右值引用类型,自然就匹配了第二个重载版本

template<typename T>
void fun(const T& val) {
cout << "l" << endl;
}

template<typename T>
void fun(T&& val) {
cout << "r" << endl;
}

template<typename T>
void test(T&& param) {
fun(forward<T>(param));
}

int main() {
const int a = 1;
test(a); // forward返回const左值引用属性
test(1);
return 0;
}

C++11 move和forward实现原理_开发语言_02

template<typename T>
void fun(T& val) {
cout << "l" << endl;
}

template<typename T>
void fun(T&& val) {
cout << "r" << endl;
}

template<typename T>
void test(T&& param) {
fun(forward<T>(param));
}

int main() {
int a = 1;
test(a); // forward返回左值引用属性
test(1);
return 0;
}

C++11 move和forward实现原理_开发语言_02


举报

相关推荐

0 条评论