If you defer work to another template function, you can use the output of the template argument to T :: bar to figure this out.
// templated on the original type, and the return type of the function template <typename T, typename mem_fn_return_type> void doThePush (T& instance, mem_fn_return_type (T::*barptr)(void)) { std::vector<mem_fn_return_type> v; v.push_back((instance.*barptr)()); v.push_back((instance.*barptr)()); v.push_back((instance.*barptr)()); std::cout << v.size() << std::endl; } template <typename T> void foo(T x) { doThePush(x, &T::bar); }
However, if you need a type in several places, it's probably best to use methods in one of the other answers.
source share