Reputation: 4959
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.
Upvotes: 1
Views: 103
Reputation: 4959
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 */ };
}
Upvotes: 0
Reputation: 41760
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.
Upvotes: 1