C++ use function argument type for template argument deduction -
template<typename t> void f(void (*func)(const t&)) { (*func)(t()); } void func(const int& i) { std::cout << << std::endl; } int main() { f(&func); }
here template argument t (= int)
of f
automatically deducted based on first argument of function func
.
can extended work general functors (lambda functions or other function objects.) possibly second function, i.e. like
template<typename t, typename function> void f(function func); template<typename function> void call_f(function func) { using arg_type = first_argument_type_t<function>; // ??? f<arg_type, function>(func); }
the fall func
in f
should able inlined compiler, std::function
cannot used.
i use function traits code this (gpl-3 licensed) that:
template <typename f> using first_argument_type_t = typename sharemind::functiontraits<f>::template argument<0u>::type;
but there's boost function_traits
alternative, might of help.
Comments
Post a Comment