10

I'm wondering if it's possible to pass a template function (or other) as an argument to a second function (which is not a template). Asking Google about this only seems to give info about the opposite ( 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 helpful)

I'm expecting 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 later call

execute(1,print);

So, can this be done or would another template have to be defined for execute() ?

Community
  • 1
  • 1
Luke Naylor
  • 103
  • 1
  • 6

2 Answers2

14

Function templates represent an infinite overload set, so unless you have a target type that is compatible with a specialization, deduction of the function type always fails. For example:

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
}

From above we can see that the validity of the program demands that we specify the template arguments for the function template, when in general it isn't always intuitive to specify them. You can instead make print a functor with a generic overloaded call operator as an extra level of indirection:

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

Now you can have execute accept any Callable and invoke it with the input:

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 (C++14) make this a lot more concise:

execute(1, [] (auto&& x) { std::cout << x; });
David G
  • 94,763
  • 41
  • 167
  • 253
  • sweet, is there a reason why the last example is compilable and not ` void print(auto x){std::cout< – Luke Naylor Jun 16 '15 at 16:43
  • @LukeNaylor `auto` is not allowed within function parameters, only in lambda declarations. Even if it were, it would be the same as the template version you had in your original example. The reason the lamda works is because it creates a functor like the one I showed you in my second code patch. – David G Jun 16 '15 at 17:04
  • Remark: in this case `execute` is not a "normal" function, it's a function template. However changing `Op` to `std::function<...>` will make it a normal function (at the cost of runtime overhead). – user202729 Feb 01 '21 at 06:39
0

Execute would need to be a template- there's no way for the compiler to create a single version of execute that would work for any input type. Now if you specified what N is for this specific function- for example if you made the second parameter print- then it should be legal.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127