Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disambiguating ambiguous function overloads because of converting constructor

Given a wrapper type

template <typename T>
struct Ptr
{
    Ptr(T*);
};

a class hierarchy

struct Base {};
struct Derived : Base {};

and a set of function overloads

void f(Ptr<Base>);
void f(Ptr<Derived>);

void g(Ptr<Base>);

is there a way to make the statements

f(new Base);
f(new Derived);
g(new Derived);

compile without changing the declarations of f or g or the call sites?

Demonstration of compilation error: https://godbolt.org/z/renKr7P1v

like image 459
Michael Deom Avatar asked Dec 22 '25 22:12

Michael Deom


1 Answers

compile without changing the declarations of f or g or the call sites?

I don't know if adding an extra overload break your requirements, but adding

template <typename T>
void f(T* p)
{
    f(Ptr{p});
}

makes code compile Demo

like image 192
Jarod42 Avatar answered Dec 24 '25 12:12

Jarod42