Putting alternate versions of typecasts in your code.

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?
 
V

Victor Bazarov

Starx said:
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?

Implement

operator double() const;

in your class that has 'getDouble', in terms of its 'getDouble'. Or
implement a wrapper class that would use the 'getDouble' and itself
provide the 'operator double' function. Then specialise your 'getAverage'
on that wrapper.

V
 
P

Phlip

Starx said:
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?

You are thinking too hard. (Yes, sometimes C++ requires that, but not now.)

Firstly, constrain your template (possibly with a trait or policy, if you
want to think too hard), to only accept types that have a conversion from
double.

Add to your types with a getDouble() this:

operator double () const { return getDouble(); }

That conversion operator lets a type silently convert to a double when
challenged by a double context. Now write the template like this:

template <class numericType>
double getAverage(const numericType& a, const numericType& b)
{
return (a + b) / 2.0;
}

The 2.0 is a double, which promotes the other terms to doubles.

And note that C++ is not an interpreted language, so you can't catch() a
compiler error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top