zoukankan      html  css  js  c++  java
  • std::forward和std::move

    std::forward完美转发

    保证参数原来的属性(用在template的参数是引用的时候):左值引用在被转发之后仍然保持左值属性,右值引用在被转发之后依然保持右值属性

    void show(int& num) {
        std::cout<<"lvalue:"<<num<<std::endl;
    }
    
    void show(int&& num) {
        std::cout<<"rvalue:"<<num<<std::endl;
    }
    
    
    template<typename T>
    void WrapperForward(T&& t) {
        show(std::forward<T>(t));
    }
    
    template<typename T>
    void WrapperNoForward(T&& t) {
        show(t);
    }
    
    void TestForward() {
        std::cout<<__FUNCTION__<<std::endl;
        int num1=10;
        WrapperForward(num1);
    
        WrapperForward(20);
    }
    
    void TestNoForward() {
        std::cout<<__FUNCTION__<<std::endl;
        int num1=10;
        WrapperNoForward(num1);
    
        WrapperNoForward(20);
    }
    

    std::move

    将左值转化为右值

    void TestMove() {
        std::cout<<__FUNCTION__<<std::endl;
        int num3=30;
        WrapperForward(std::move(num3));
    }
    
    void TestNoMove() {
        std::cout<<__FUNCTION__<<std::endl;
        int num3=30;
        WrapperForward(num3);
    }
    

  • 相关阅读:
    Docker contanier comunication with route
    Event Sourcing
    Event Sourcing
    Event Sourcing
    .Net async
    安装Docker
    【JQuery】数据
    【JQuery】遍历
    【JQuery】css操作
    【JQuery】文档操作
  • 原文地址:https://www.cnblogs.com/smallredness/p/11085369.html
Copyright © 2011-2022 走看看