why can't bind be turned into function?

the problem is that IDE says the bind cannot be turn into function pointer.

Fudge<int> obj(1);

std::function<void(std::vector<int>&)> a = std::bind(&Fudge<int>::_pack_method, obj); //IDE warning here

where the _pack_method is declared as

template <class E>
class Fudge
{
public:
 Fudge() = delete;
 Fudge(int i) {};
 void _pack_method(std::vector<E>& frames) {return;};
}

Upvotes: 0

Views: 68

Answers (1)

NathanOliver
NathanOliver

Reputation: 181027

Your problem here is you are not accounting for the function parameter of _pack_method. You need to tell bind how to fully bind with the function and you don't do that, you only give it the object to call the function on and not the vector parameter that it takes. To fix this, you either need to provide the vector you want to pass to the member function like

std::bind(&Fudge<int>::_pack_method, obj, my_vector); 

or you need to use a placeholder to tell bind that that the function object that is created needs to take in parameter in order for it to work. That would look like

std::bind(&Fudge<int>::_pack_method, obj, std::placeholders::_1); 

Instead of using bind, you could use a lambda expression which I find to be more explicit in denoting what is going on. That would look like:

auto a = [&obj](std::vector<int>& var){ obj._pack_method(var); };

Upvotes: 2

Related Questions