#include <thread>
#include <iostream>
using namespace std;
void func(int& i){
cout<<++i<<endl;
}
int main(){
int x=7;
thread t(func,x);
t.join();
return 0;
}
得到一個錯誤約
error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
我明白,我不能這樣做,但thread(func, 4)
x
是一個變量,而不是暫時的。
我用gcc 4.7與-std = C++ 11 -pthread
爲什麼發生這個錯誤?
看起來像實施中的錯誤。 – bames53 2013-03-05 23:24:50
@ bames53,此行爲是標準要求的。 – 2013-03-06 00:30:56
@JonathanWakely啊,我明白了。我快速瀏覽了'std :: thread'的定義,並看到了完美的轉發,但我並沒有停下來想起/考慮DECAY_COPY的意義。 – bames53 2013-03-06 01:07:12