zoukankan      html  css  js  c++  java
  • c/c++ 右值引用,forward关键字

    c++ forward关键字

    forward的由来:保持住参数的右值属性。

    模板函数中的推导类型,作为另一函数的参数时,不管实参是什么类型,作为另一个参数的实参时,都变成了左值。因为C++里规定函数的形参就是左值,不管调用侧的实参是否是右值。所以,调用的另一个函数的形参即使用T&& arg来声明,传过去的也是左值,编译不过,因为不能自动把左值转化成右值,除非使用std::move。forward就是为了解决这个问题的。

    forward() 函数的作用:保持住实参的类型。
    
    include <iostream>
    using namespace std;
    
    void rvalue_call(int& v){
      cout << "& call" << endl;
    }
    void rvalue_call(int&& v){
      cout << "&& call" << endl;
    }
    void rvalue_call(const int& v){
      cout << "const & call" << endl;
    }
    void rvalue_call(const int&& v){
      cout << "const && call" << endl;
    }
    
    template<typename T>
    void func(T&& a){
      rvalue_call(a);
    }
    
    int main(void){
      int x = 1;
      func(x);//实参为左值                                           
      int& y = x;
      func(y);//实参为左值引用                                       
      func(std::move(y));//实参为右值引用                            
      func(100);//实参为右值引用          
      const int a = 11;
      func(a);//实参为左值常引用   
      func(std::move(a));//实参为右值常引用   
    }
    

    执行结果:

    & call
    & call
    & call
    & call
    const & call
    const & call
    

    上面的例子即使传递的是右值,但也不能够调用到

    void rvalue_call(int&& v)
    
    void rvalue_call(const int&& v)
    

    解决办法:加std::forward

    #include <iostream>
    using namespace std;
    
    void rvalue_call(int& v){
      cout << "& call" << endl;
    }
    void rvalue_call(int&& v){
      cout << "&& call" << endl;
    }
    void rvalue_call(const int& v){
      cout << "const & call" << endl;
    }
    void rvalue_call(const int&& v){
      cout << "const && call" << endl;
    }
    
    template<typename T>
    void func(T&& a){
      rvalue_call(std::forward<T> (a));
    }
    
    int main(void){
      int x = 1;
      func(x);//实参为左值                                           
      int& y = x;
      func(y);//实参为左值引用                                       
      func(std::move(y));//实参为右值引用                            
      func(100);
        
      const int a = 11;
      func(a);
      func(std::move(a));
    }
    

    执行结果:发现可以调用到右值的两个函数。这就是std::forward函数在模板里的作用

    & call
    & call
    && call
    && call
    const & call
    const && call
    

    另一个例子:

    #include <iostream>
    #include <utility>
    
    template<typename F, typename T1, typename T2>
    void fcn2(F f, T1&& t1, T2&& t2){
      f(std::forward<T2>(t2), std::forward<T1>(t1));//OK
      //f(std::move(t2), std::forward<T1>(t1));//OK
      //f(t2, t1);//ERROR
    }
    
    void f1(int&& i1, int& i2){
      i1 = 10;
      i2 = 20;
    }
    
    
    
    int main(){
      int i1 = 1, i2 = 2;
      int& a = i1;
      int& b = i2;
      int&& c = 111;
    
      fcn2(f1, i1, 42);//因为42为右值,所以fcn2的T2为右值,如果不加forward,把T2的形参传给另一个函数时,它就变成了左值,但是函数f1的参数时右值,这时,编译就不过了。
      std::cout << i1 << ", " << i2 << std::endl;
    
    }
    
    

    c/c++ 学习互助QQ群:877684253

    本人微信:xiaoshitou5854

  • 相关阅读:
    selenium 手写xpath
    pythonselenium面试常问问题
    pytest介绍
    pytest + allure 生成自测报告
    python自动化接口面试题
    接口测试面试题汇总
    Eclipse 视图与视窗
    Access数据库密码为不可见字符的处理方式
    eclipse 导出jar 没有主清单属性的解决方法
    如何在 Windows 下编译 OpenSSL VS2013
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9589008.html
Copyright © 2011-2022 走看看