Say you have 2 free functions:
void do_something(dog d);
void do_something(cat c);
No say you want to pass these functions to a templated function:
template <typename DoSomethingFunc>
void do_something_template(DoSomethingFunc func);
What would be the preferred way to call do_something_template
in a manner that avoids overload resolution conflicts? Would it be casting?
You can overload both member functions and free functions.
The call is resolved for an overloaded subroutine by an instance of function through a process called Argument Matching also termed as the process of disambiguation.
In Function overloading, sometimes a situation can occur when the compiler is unable to choose between two correctly overloaded functions. This situation is said to be ambiguous. Ambiguous statements are error-generating statements and the programs containing ambiguity will not compile.
You could cast or use a local function pointer variable.
void (*p)(dog) = do_something;
do_something_template(p);
do_something_template(static_cast<void(*)(cat)>(do_something));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With