Search code examples
c++overload-resolution

Overload resolution in case of templates and inheritance in unique_ptr


I have two classes:

class Base {};
class Derived : public Base {};

And two overloaded functions:

void call(std::unique_ptr<Base> op)
{
    std::cout << "First overloading" << std::endl;
}

template<class F>
void call(F f) 
{
    std::cout << "Second overloading" << std::endl;
}

Let's call it with unique_ptr of Derived

call(std::make_unique<Derived>());

I expect calling first function, however second is called instead. Why compiler choose generic function instead of specific one? Overload resolution does not support inheritance and polymorphism? If so, how I can fix it and call first function?


Solution

  • Both of your overloads are valid candidates. If you remove either, the remaining one is still a valid overload. In such a case, broadly speaking, the overload that will be chosen will be the one that most closely matches the arguments provided.

    You provided std::unique_ptr<Derived>. In the first case, it expects std::unique_ptr<Base>. There exists an implicit conversion from std::unique_ptr<Derived> to std::unique_ptr<Base>, so this overload is legal, but it does require a conversion.

    In the second case, F is simply deduced to be std::unique_ptr<Derived>. It does not require a conversion and is a closer match to the argument provided. Therefore, it is preferred.

    Edit: It seems I missed the part about a fix.

    You can make the overload that accepts a unique_ptr a function template instead. This way, you can get an exact match with the first overload, eliminating the need for a conversion. You can use std::enable_if to disable that overload for unique_ptrs of incompatible types:

    #include <memory>
    #include <type_traits>
    
    class Base {};
    class Derived : public Base {};
    
    template<class T>
    std::enable_if_t<std::is_base_of_v<Base, T>>
    call(std::unique_ptr<T>);
    
    template<class T>
    void call(T);