ambiguous overloaded function differs only by argument's template parameter

105 Views Asked by At

Consider the following code:

SmartPointer<Data> Fix(SmartPointer<Data> data)
{
    return { /* Fixed Data */ };
}
SmartPointer<Data> Fix(SmartPointer<DataWrapper> dataWrapper)
{
    return Fix(dataWrapper->Data());    
}

How would I rewrite this so that it does not cause "error C2668: ambiguous call to overloaded function" ?

Note: I would like to be able to pass in a subclass for example SmartPointer<SubclassOfDataWrapper> as well, and have that resolve to the overloaded function of the superclass.

2

There are 2 best solutions below

6
Patrick Parker On BEST ANSWER

Thanks to the hint provided by Guillaume Racicot, I came up with the following solution:

template<typename T>
SmartPointer<Data> Fix(SmartPointer<T> dataWrapper)
{
    // Note: only a subclass of DataWrapper would have the method ->Data()
    //            a subclass of Data would not have it (SFINAE principle)
    return Fix( dataWrapper->Data() );
}
template<>
SmartPointer<Data> Fix(SmartPointer<Data> data)
{
    return { /* Fixed Data */ };
}
1
Guillaume Racicot On

Check if a there is a template conversion operator in your smart pointer class.

More importantly, check if there's something to SFINAE-out the conversion operator in case the conversion is invalid.

A static assert inside the conversion operator won't tell the compiler these overload are not ambiguous.