Suppose there are function overloads for int
and double
.
void Print(int v)
{
cout << v << endl;
}
void Print(double v)
{
cout << v << endl;
}
There is function which is intended to pass in as callable one of above functions.
template<typename FnT>
void Function(int v, FnT&& fn)
{
fn(v);
}
But following code is ambiguous:
Function(1, Print);
Compiler fails to deduce type for second argument. This can be easily solved:
Function(1, static_cast<void(*)(int)>(Print));
I believe exists more generic and elegant way to solve the issue. The issue can be raised up in STL algorithm, when function overload is added.
vector<int> v = { 1,2,3 };
for_each(v.begin(), v.end(), Print); // ambiguous code
How to solve this in a pretty way?