c 11 為什麼使用ref,和引用的區別

2022-03-26 03:12:59 字數 831 閱讀 4395

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

其中代表的例子是thread

比如thread的方法傳遞引用的時候,必須外層用ref來進行引用傳遞,否則就是淺拷貝。

執行緒函式的引數按值移動或複製。如果引用引數需要傳遞給執行緒函式,它必須被包裝(例如使用std :: ref或std :: cref)。

#include #include #include#include#include#include#include#include#include #include void method(int & a)

using namespace std;

int main()

int main()

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

其中代表的例子是thread

比如thread的方法傳遞引用的時候,必須外層用ref來進行引用傳遞,否則就是淺拷貝。

執行緒函式的引數按值移動或複製。如果引用引數需要傳遞給執行緒函式,它必須被包裝(例如使用std :: ref或std :: cref)。

#include #include #include#include#include#include#include#include#include #include void method(int & a)

using namespace std;

int main()

int main()

c 11之左值引用和右值引用

c 11中增加了右值引用和move語義來避免一些不必要的構造和copy操作,以此來提公升程式的執行效率。首先說左值和右值,他們絕不是簡單的等號左邊和右邊的區別,總結來說 1 左值可以定址,而右值不可以。2 左值可以被賦值,右值不可以被賦值,可以用來給左值賦值。3 左值可變,右值可變 僅對基礎型別適用...

C 11中的左值引用和右值引用

1.首先區分左值和右值 左值是表示式結束後依然存在的持久物件 右值是表示式結束時就不再存在的臨時物件 舉例 int a 10 int b 20 int pflag a vectorvcttemp vcttemp.push back 1 string str1 hello string str2 wo...

C11編輯器公升級和C11標準使用

c11中的一些特性需要對應的編譯器才能支援。而有些系統預設的編譯器並不支援c11,所以需要4.8及其以上的版本。以下採用c11才支援的std unordered set來進行測試。include include include include include using namespace std ...