c++templatestemplate-argument-deductionconstructor-overloadingdeduction-guide

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


Solution

  • 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