c++ - Using std::bind to bind parameters and object instance separately -
is possible bind arguments member function call, , later bind target object separately?
what trying achieve helper function receives function arbitrary arguments argument, , executes on items in collection.
void sometype::work(uint a, uint b, uint c) { //do } template<typename function, class container> void execute(const function& fn, const container& collection) { for(auto& item : collection) { auto bound = std::bind(fn, &item); bound(); } } void test() { //eg. container = vector of sometype auto fn = std::bind(&sometype::work, 10, 20, 30); execute(fn, container); }
this failing error within functional:
error c2064: term not evaluate function taking 3 arguments
eventually pointing at:
see reference function template instantiation 'void execute<std::_bind<true,void,std::_pmf_wrap<void (__thiscall sometype::* )(uint,uint,uint),void,sometype,uint,uint,uint>,uint &,uint &,uint &>>(const function &)' being compiled [ function=std::_bind<true,void,std::_pmf_wrap<void (__thiscall sometype::* )(uint,uint,uint),void,sometype,uint,uint,uint>,uint &,uint &,uint &> ]
i worked around passing in lambda captures desired values , executes desired function on object passed it. i'm still left wondering if binding functions way has legs , going wrong, or if it's not doable.
you should leave placeholder target object bound @ 2nd time binding, when bind member function sometype::work()
.
try this:
using namespace std::placeholders; auto fn = std::bind(&sometype::work, _1, 10, 20, 30); ~~
Comments
Post a Comment