c++ - Short way to std::bind member function to object instance, without binding parameters -
i have fellow member function several arguments. i'd bind specific object instance , pass function. can placeholders:
// actualinstance myclass* auto callback = bind(&myclass::myfunction, actualinstance, _1, _2, _3);
but bit clumsy - one, when number of parameters changes, have alter bind calls well. in addition, it's quite tedious type placeholders, when want conveniently create "function pointer" including object reference.
so i'd able like:
auto callback = objectbind(&myclass::myfunction, actualinstance);
does know nice way this?
i think work:
template<typename r, typename c, typename... args> std::function<r(args...)> objectbind(r (c::* func)(args...), c& instance) { homecoming [=](args... args){ homecoming (instance.*func)(args...); }; }
then:
auto callback = objectbind(&myclass::myfunction, actualinstance);
note: you'll need overloads handle cv-qualified fellow member functions. ie:
template<typename r, typename c, typename... args> std::function<r(args...)> objectbind(r (c::* func)(args...) const, c const& instance) { homecoming [=](args... args){ homecoming (instance.*func)(args...); }; }
c++ stdbind
No comments:
Post a Comment