An explanation of why this does not work was given by Angew and Urzeit .,
What I'm trying to offer is a possible solution to the problem. However, I cannot say for sure whether this is suitable for your case, as the OP offers limited information about your specific design requirements.
The first step is to convert f1 and f2 into template template classes:
template <typename T> class f1 { public: void operator ()( T t ) { std::cout << "f1( " << t << " ) called." << std::endl; } };
(Similarly for f2 .) This way you can pass f1 (not f1<T> ) as a template template parameter to call , which is now defined this way:
template <template <typename> class F, typename T> void call( T t ) { F<T> f; f( t ); }
Note that F is a template template parameter that binds to a template class with one template type parameter (for example, f1 ).
Finally, foo will be as follows:
template <typename T> void foo( T t ) { call<f1>( t ); call<f2>( t ); }
bar remains as before.
source share