Here is the problem I am facing: I have an overloaded function in a class, and I want to pass one of its overloads as a parameter. But when doing so, I get the following error :
"no suitable constructor exists to convert from <unknown-type> to std::function<...>"
Here's a code sample to illustrate that :
#include <functional>
#include <string>
class Foo
{
private:
int val1 , val2;
};
class Bar
{
public:
void function ( ) {
//do stuff;
Foo f;
function ( f );
}
void function ( const Foo& f ) {
//do stuff
}
private:
//random attribute
std::string str;
};
void otherFunction ( std::function<void ( Bar& , const Foo& ) > function ) {
Bar b;
Foo f;
function(b,f);
}
int main ( ) {
otherFunction ( &Bar::function );
^^^
error
}
I understand that the compiler cannot deduce which overload to use, so the next best thing to do is a static_cast
, but the following code still has the same error
std::function<void ( Bar& , const Foo& )> f = static_cast< std::function<void ( Bar& , const Foo& )> > ( &Bar::function );