c++c++11overloadingsfinaeambiguous-call

ambiguous overloaded function differs only by argument's template parameter


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.


Solution

  • 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 */ };
    }