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?
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_ptr
s 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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With