S
Starx
Say I had the following function:
template <class numericType>
double getAverage(const numericType& a, const numericType& b)
{
return double(a + b) / 2;
}
This will work fine with any types that the double() typecast works on.
But I'd like it to also work on types that don't have a double()
typecast, they have a getDouble() member function. First I tried a
try/catch:
template <class numericType>
double getAverage(const numericType& a, const numericType& b)
{
try {
return double(a + b) / 2;
} catch (...) {
return (a.getDouble() + b.getDouble()) / 2;
}
}
Turns out that doesn't work, it won't compile if the type doesn't have
both the getDouble() member function and the double() typecast. Is
there any way of getting this to work? Any workarounds you could
suggest?
template <class numericType>
double getAverage(const numericType& a, const numericType& b)
{
return double(a + b) / 2;
}
This will work fine with any types that the double() typecast works on.
But I'd like it to also work on types that don't have a double()
typecast, they have a getDouble() member function. First I tried a
try/catch:
template <class numericType>
double getAverage(const numericType& a, const numericType& b)
{
try {
return double(a + b) / 2;
} catch (...) {
return (a.getDouble() + b.getDouble()) / 2;
}
}
Turns out that doesn't work, it won't compile if the type doesn't have
both the getDouble() member function and the double() typecast. Is
there any way of getting this to work? Any workarounds you could
suggest?