It seems I should to use function as a template argument, but it cannot be done in such way.
The header file looks like this:
foo_out_type foo(foo_in_type input);
baz_out_type bar(bar_in_type input);
template<typename T_IN, typename T_OUT>
void do_list(const vector<T_IN>& input,
vector<T_OUT>& output);
// I want to specialize the template code at compile time
// So the users don't need to compile the code everytime
template<>
void do_list<foo_in_type, foo_out_type>(const vector<foo_in_type>& input,
vector<foo_out_type>& output);
template<>
void do_list<bar_in_type, bar_out_type>(const vector<bar_in_type>& input,
vector<bar_out_type>& output);
The implementation file looks like this:
template<typename T_IN, typename T_OUT, typename T_FUNC>
void __do_list_impl__(const vector<T_IN>& input,
vector<T_OUT>& output)
{
for (size_t i=0; i<input.size(); i++)
output.push_back(T_FUNC(input[i]));
}
template<>
void do_list<foo_in_type, foo_out_type>(const vector<foo_in_type>& input,
vector<foo_out_type>& output)
{
__do_list_impl__<foo_in_type, foo_out_type, do_foo>(input, output);
}
template<>
void do_list<bar_in_type, bar_out_type>(const vector<bar_in_type>& input,
vector<bar_out_type>& output)
{
__do_list_impl__<bar_in_type, bar_out_type, do_bar>(input, output);
}
The key is: I don't want to write the implementation several times, as the actual code is much more complex than this. However, the function cannot be passed as an template argument.
So, how should I work around this?