• C++11 move和forward实现原理


    一、move与forward的作用

    • move:不能移动任何东西,它唯一的功能是将一个左值强制转化为右值引用,继而可以通过右值引用使用该值,以用于移动语义,从实现上讲,std::move基本等同于一个类型转换:static_cast(lvalue)

    • forward: 不转发任何东西,也是执行左值到右值的强制类型转换,只是仅在特定条件满足时才执行该转换
      典型使用场景:某个函数模板使用了万能引用类型为形参,随后把它传递给了另一个函数

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

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

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

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

    string s("hello");
    std::move(s);
    
    • 1
    • 2

    以上代码中传递给move函数的实参类型是string&,形参为_Ty&& _Arg,即_Ty&&应该是string&,故模板类型_Ty实例化为string&,move函数如下:

    return static_cast<remove_reference_t<string&>&&>(_Arg);
    
    • 1

    remove_reference_t会匹配第二个重载形式,remove_reference_t的结果就是string

    return static_cast<string&&>(_Arg);
    
    • 1

    这样move函数就得到了string&&

    三、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));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    形参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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

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

    int val = 7;
    fun(val);
    fun(47);
    
    • 1
    • 2
    • 3

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

    对于fun(7),传入的是一个右值, 那么fun中T的类型将是int,param类型是int&&。因此,在std::forward模板函数中,推断出_Ty的类型为int,std::remove_reference用int进行实例化,std::remove_reference的type成员是int,在forward源码中有static_cast,则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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述
    对于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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

  • 相关阅读:
    FPGA面试题(5)
    网络安全(黑客技术)自学
    成人自考-英语二-形容词
    编译zlmediakit记录
    Spring6--IOC反转控制 / 基于XML管理bean
    JS——经典案例
    3D人物建模用哪个软件入门比较快?
    Linux 安装ssh和配置ssh
    互斥锁(下):如何用一把锁保护多个资源?
    信息系统项目管理师必背核心考点(五十七)知识管理工具
  • 原文地址:https://blog.csdn.net/qq_42500831/article/details/127602080