I think everyone has experience working with a code like the following:
void fun(Type1&);
void fun(Type2&);
vector<Type1> vec;
for_each(vec.begin(), vec.end(), fun);
Of course that won’t compile, because it’s not clear which function to be passed. And what’s your commonly-used solution to the problem?
I know this will work:
for_each(vec.begin(), vec.end(), (void(*)(Type1&))fun);
But any better ideas?
One solution is to use template function:
The better way is to use functor with template
operator():