class Test{
public:
int work(){
cout << "in work " << endl;
return 0;
}
void work(int x){
//cout << "x = " << x << endl;
cout << "in work..." << endl;
}
};
int main(){
Test test;
std::function<void()> f = std::bind(&Test::work, &test);
thread th(f);
th.join();
return 0;
}
正如上面的代碼,我想結合一類的成員函數void work(void)
(我們將其命名爲測試) ,但會發生編譯器錯誤,表示無法確定使用哪個重載函數。如何綁定同名的成員函數的類,使用C++ 11的std ::綁定
我不能改變class Test因爲它屬於一個lib,如何實現我的目標?提前致謝!
首選'auto f = [&test] {test.work(); };'對任何綁定表達式 –
lambda確實比std :: bind方便實現我的目標,看到這些答案後 – debugman
還要注意,通過使用'std :: function'而不是'auto',您支付了type-擦除 –