我對gcc 4.5.2有一個有趣的問題。下面的代碼啓動std :: thread時無法綁定(函數指針)左值爲(函數指針)右值?
#include<thread>
#include<iostream>
using std::cout;
void foo(int a){
cout<<a;
}
template <typename T>
void goo(void (*fn)(T),T c){
fn(c);
}
int main(void)
{
std::thread TH;
void (*ptr)(int)=foo;
TH= std::thread(goo<int>,ptr,1);
TH.join();
return 0;
}
..不會error: cannot bind ‘void(void (*)(int), int)’ lvalue to ‘void (&&)(void (*)(int), int)’
其次是第二個錯誤initializing argument 1 of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void(void (*)(int), int), _Args = {void (*&)(int), int}]’
但是在GCC 4.5.2編譯,當template
被刪除這段代碼確實編譯,它也確實與編譯gcc 4.7.0(已有template
)。
即使這是一個編譯器問題,有人能解釋一下這樣的錯誤是什麼意思嗎?我會很高興找到一種方法來進行綁定(即使這是在gcc 4.7中自動執行的)。
爲什麼要支持GCC 4.5.2?如果你一般都在做這種事情,那麼這個程序的其他部分是否仍然可能會違反它的限制呢? – Potatoswatter
我正在編寫CUDA/C++代碼,可以通過預處理器標誌進行調整;沒有更多的C++ 11被發現在代碼中的任何地方('nvcc'不支持它..所以我堅持4.5.2進行編譯,不喜歡改變編譯器只是因爲這個有趣的問題出現在CPU的情況下... –
在gcc上工作4.7.0 – pmr