Template functions cannot be passed as template arguments. You must manually infer the template arguments for this function before passing it to another template function. For example, you have a function
T sum(T a, T b) { return a + b; }
You want to pass it to callFunc:
template<typename F, typename T> T callFunc(T a, T b, F f) { return f(a, b); }
You can't just write
int a = callFunc(1, 2, sum);
You need to write
int a = callFunc(1, 2, sum<int>);
To be able to pass the sum without an int record, you need to write functor - struct or class with operator (), which will call your template function. Then you can pass this functor as a template argument. Here is an example.
template<class T> T sum(T a, T b) { return a + b; } template<class T> struct Summator { T operator()(T a, T b) { return sum<T>(a, b); } }; template<template<typename> class TFunctor, class T> T doSomething(T a, T b) { return TFunctor<T>()(a, b);
izogfif
source share