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

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -