Automatic type conversion to complex<T>?

M

MWimmer

Dear readers of this discussion group,

I have a problem concerning user defined type conversion and
functions, that are only defined as templates. Consider the following
code example:

-----------------------------------------------------------------------

#include <complex>

using namespace std;

template<typename T>
class A {
public:
operator T() const
{
return T();
}
};

complex<double> f(const complex<double> &_a)
{
}

template<typename T>
complex<T> g(const complex<T> &_a)
{
}

main()
{
A<complex<double> > a;

complex<double> b=f(a); //OK
complex<double> c=g(a); //error: no matching function for call
// to 'g(A<std::complex<double> >&)'
}

-------------------------------------------

In this example, the compiler is not able to call the type conversion
function of class A int he case of function g, as it is only defined
as a template.

Unfortunately the code snippet mimics a real problem that I have: a
template class, that can be useful for double and complex<double> and
that should have an automatic type conversion to the respective type.
Now all the useful functions for complex<T> are defined as is function
g in my example, e.g. I cannot call A<complex<double> > a; sin(a) ,
wehreas A<double> a2; sin(a2) is OK.

Is there a way to circumvent this problem without writing things like
sin(complex<double>(a)) ?

Best regards,

Mike Wimmer
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top