c++ - How to pass a template function as template class argument? -


here's thing, seems it's possible pass templatized struct contains static functions this:

template <class t> struct functionholder {};  template <> struct functionholder<string> {     static void f(const string &s) {         cout << "f call " << s << endl;     } };  template <class key, class value, class holder = functionholder<key>> class foo {   public:     foo(key k) {         holder::f(k);     } };  int main(int argc, char *argv[]) {     foo<string, int> foo = foo<string, int>("test_string"); } 

but, possible pass directly templatized functions without been defined statically on templatized structs? i've tried , won't compile:

template <class string> static void f(const string &s) {     cout << "f call " << k << endl; }  template <class key, class value, typename func<key>> class foo {   public:     foo(key k) {         func(k);     } };  int main(int argc, char *argv[]) {     foo<string, int> foo = foo<string, int>("test_string"); } 

asking cos it's not cool forced create dummy structures (structures containing bunch of static functions) used template type of main class.

unfortunately function templates can't used template template arguments; can use function pointer non-type template parameter instead, e.g.

template <class key, class value, void(*func)(const key&) = f<key>> class foo {   public:     foo(key k) {         func(k);     } }; 

live


Comments