Std :: bind a static member function inside a class

I am trying to save a function to be called later, here is a snippet.

This works great:

void RandomClass::aFunc( int param1, int param2, double param3, bool isQueued /*= false */ ) { /* If some condition happened, store this func for later */ auto storeFunc = std::bind (&RandomClass::aFunc, this, param1, param2, param3, true); CommandList.push( storeFunc ); /* Do random stuff */ } 

However, if RandomClass is static, so I believe I should do this:

 void RandomClass::aFunc( int param1, int param2, double param3, bool isQueued /*= false */ ) { /* If some condition happened, store this func for later */ auto storeFunc = std::bind (&RandomClass::aFunc, param1, param2, param3, true); CommandList.push( storeFunc ); /* Do random stuff */ } 

But this does not work, I get a compilation error

error C2668: 'std :: tr1 :: bind': ambiguous call to an overloaded function

Any help was appreciated.

+6
source share
1 answer

A pointer type to a static member function looks like a pointer to a non-member fyinction:

 auto storeFunc = std::bind ( (void(*)(WORD, WORD, double, bool)) &CSoundRouteHandlerApp::MakeRoute, sourcePort, destPort, volume, true ); 

Here is a simplified example:

 struct Foo { void foo_nonstatic(int, int) {} static int foo_static(int, int, int) { return 42;} }; #include <functional> int main() { auto f_nonstatic = std::bind((void(Foo::*)(int, int))&Foo::foo_nonstatic, Foo(), 1, 2); auto f_static = std::bind((int(*)(int, int, int))&Foo::foo_static, 1, 2, 3); } 
+14
source

All Articles