Reputation: 9863
Here's the thing, it seems it's possible to pass a templatized struct which contains some static functions like 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, would it be possible to pass directly templatized functions without been defined statically on templatized structs? I've tried this and it 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 this cos it's not cool to be forced to create dummy structures (structures containing a bunch of static functions) to be used as template type of the main class.
Upvotes: 1
Views: 154
Reputation: 173014
Unfortunately function templates can't be used as template template arguments; you can use function pointer as the 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);
}
};
Upvotes: 2