Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast overloaded free function to resolve overload conflict?

Tags:

c++

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?

like image 407
Graeme Avatar asked Jul 30 '13 08:07

Graeme


People also ask

Can free functions be overloaded?

You can overload both member functions and free functions.

How are function calls matched with overloaded 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.

How can call to an overloaded function be ambiguous?

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.


1 Answers

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));
like image 134
CB Bailey Avatar answered Oct 07 '22 06:10

CB Bailey