In c++ 11, how to invoke an arbitrary callable object? -
the concept of callable defined in http://en.cppreference.com/w/cpp/concept/callable.
suppose have callable object f has 1 argument of type t* , return type void. f can callable type (a function object, pointer member function, pointer data member, etc). how can invoke f?
simply calling f(x) fails since f can pointer member function or data member. there simple way call f? 1 possible solution std::bind(f, x)(), solution becomes more complex when f has more arguments.
this std::invoke
does, won't standard until c++17. can make own version, can pretty complicated if general.
here's basic idea 2 cases (code taken cppreference.com):
template <class f, class... args> inline auto invoke(f&& f, args&&... args) -> decltype(std::forward<f>(f)(std::forward<args>(args)...)) { return std::forward<f>(f)(std::forward<args>(args)...); } template <class base, class t, class derived> inline auto invoke(t base::*pmd, derived&& ref) -> decltype(std::forward<derived>(ref).*pmd) { return std::forward<derived>(ref).*pmd; }
Comments
Post a Comment