Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 785
NikBond Avatar asked May 30 '19 14:05

NikBond


2 Answers

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<Base, T>::value>>
call(std::unique_ptr<T>);

template<class T>
void call(T);
like image 140
François Andrieux Avatar answered Sep 17 '22 16:09

François Andrieux


The problem is, the 2nd overload is an exact match when T being deduced as std::unique_ptr<Derived>; while the 1st overload requires an implicit conversion from std::unique_ptr<Derived> to std::unique_ptr<Base>. Then the 2nd one wins in overload resolution.

You can add apply SFINAE:

template<class F>
std::enable_if_t<!std::is_convertible_v<F, std::unique_ptr<Base>>> call(F f) 
{
    std::cout << "Second overloading" << std::endl;
}

LIVE

like image 26
songyuanyao Avatar answered Sep 20 '22 16:09

songyuanyao