使用泛型STD:在一個類中具有成員函數的函數對象對于一個類,我想在一個類中存儲一些指向同一個類的成員函數的函數指針。map貯存std::function物品。但我在一開始就失敗了,這段代碼是:class Foo {
public:
void doSomething() {}
void bindFunction() {
// ERROR
std::function<void(void)> f = &Foo::doSomething;
}};我收到error C2064: term does not evaluate to a function taking 0 arguments在……里面xxcallobj結合一些奇怪的模板實例化錯誤。目前,我在Windows 8上使用VisualStudio 2010/2011,在Win 7上使用VS 10,它也失敗了。錯誤必須基于一些我不遵循的奇怪的C+規則。編輯:我知道不用助推。這是集成在MS編譯器中的C+11。
3 回答

MMMHUHU
TA貢獻1834條經驗 獲得超8個贊
std::function
<void(void)>
std::function<void(void)> f = std::bind(&Foo::doSomething, this);
using namespace std::placeholders;std::function<void(int,int)> f = std::bind(&Foo::doSomethingArgs, this, _1, _2);
std::function<void(int,int)> f = [=](int a, int b) { this->doSomethingArgs(a, b);}

拉丁的傳說
TA貢獻1789條經驗 獲得超8個贊
std::function<void(Foo*)> f = &Foo::doSomething;
this
std::function<void(void)> f = std::bind(&Foo::doSomething, this);

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
class MyClass{public: void MemberFunc(int value) { //do something }};// Store member function bindingauto callable = std::mem_fn(&MyClass::MemberFunc); // Call with late supplied 'this'MyClass myInst;callable(&myInst, 123);
std::_Mem_fn_wrap<void,void (__cdecl TestA::*)(int),TestA,int> callable
std::function<void(int)> binding = std::bind(callable, &testA, std::placeholders::_1);binding(123); // Call
- 3 回答
- 0 關注
- 597 瀏覽
添加回答
舉報
0/150
提交
取消