Passing a template function as an argument to a normal function

I am wondering if it is possible to pass a template function (or another) as an argument to a second function (which is not a template). Asking Google about this only seems to give information to the contrary ( Function passed as template argument )

The only relevant page I could find was http://www.beta.microsoft.com/VisualStudio/feedbackdetail/view/947754/compiler-error-on-passing-template-function-as-an-argument-to- a-function-with-ellipsis (not very useful)

I expect something like:

template<class N>void print(A input){cout << input;}
void execute(int input, template<class N>void func(N)){func(input)}

and then call

execute(1,print);

So, can this be done or will it be necessary to define another template for execute ()?

+4
source share
2

, , , , . :

template<class T> void f(T);
template<class T> void h(T);

void g() {
    h(f); // error: couldn't infer template argument 'T'
    h(f<int>); // OK, type is void (*)(int)
    h<void(int)>(f); // OK, compatible specialization
}

, , . print :

struct print {
     template<typename T>
     void operator()(T&& x) const {
         std::cout << x;
     }
};

execute :

template<class T, class Op>
void execute(T&& input, Op&& op) {
    std::forward<Op>(op)(std::forward<T>(input));
}

void g() { execute(1, print{}); }

Generic lambdas (++ 14) :

execute(1, [] (auto&& x) { std::cout << x; });
+4

- , . , , N , , print, .

0

All Articles