c++11 爲何使用ref,和引用的區別

std::ref只是嘗試模擬引用傳遞,並不能真正變成引用,在非模板狀況下,std::ref根本無法實現引用傳遞,只有模板自動推導類型時,ref能用包裝類型reference_wrapper來代替本來會被識別的值類型,而reference_wrapper能隱式轉換爲被引用的值的引用類型。ios

std::ref主要是考慮函數式編程(如std::bind)在使用時,是對參數直接拷貝,而不是引用編程

其中表明的例子是thread
好比thread的方法傳遞引用的時候,必須外層用ref來進行引用傳遞,不然就是淺拷貝。

app

線程函數的參數按值移動或複製。若是引用參數須要傳遞給線程函數,它必須被包裝(例如使用std :: ref或std :: cref)。函數式編程

#include <functional>
#include <iostream>
#include<cstring>
#include<string.h>
#include<memory>
#include<atomic>
#include<unordered_map>
#include<mutex>
#include <thread>
#include <string>
void method(int & a){ a += 5;}

using namespace std;
int main(){

    int a = 0;
    // each reference used by the threads would refer to the same object.
    thread th(method,ref(a));
    th.join();
    cout << a <<endl;
    /*
     *
I could compile your code successfully with MSVC2013. However, thread() works passing copies of its argument to the new thread. This means that if your code would compile on your compiler, each thread wourd run with its own copy of ht, so that at the end, main's a would be empty.
GCC doesn't compile with this weird message.
     */
    /*thread th2(method, a);  //淺拷貝
    th2.join();
    cout << a <<endl;*/
    return 0;
}

 

/** @file  bindRefT.cpp
*  @note   
*  @brief
*  @author 
*  @date   2019-8-8
*  @note   
*  @history
*  @warning
*/
#include <functional>
#include <iostream>
//std::ref主要是考慮函數式編程(如std::bind)在使用時,是對參數直接拷貝,而不是引用
void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}

int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
}
相關文章
相關標籤/搜索