mardi 6 janvier 2015

Comparing std::function for member functions

I tried to search and here similar questions:

question 1

question 2


But anyway, I can't compare member functions. Here's an example:



class ClassA
{
public:
int add(int a, int b)
{
return a + b;
}
};

int main()
{
ClassA a1{};

function<int(int, int)> f1 = bind(&ClassA::add, a1, placeholders::_1, placeholders::_2);
function<int(int, int)> f2 = bind(&ClassA::add, a1, placeholders::_1, placeholders::_2);

cout << boolalpha << "f1 == f2 " << (f1.target_type() == f2.target_type()) << endl; // true
cout << (f1.target<int(ClassA::*)(int, int)>() == nullptr) << endl; // true

return 0;
}


From the code it's obvious that f1 and f2 are different. The first cout shows true because the types are the same, it's ok. But why the second cout is true? Why function::target() returns nullptr?


P.S.: I want to create a simple delegate system so I can pass any function (global, static, member) around. With std::function I can add a callback, but I don't know how to remove it.


Aucun commentaire:

Enregistrer un commentaire