Home

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

previous
next

Re: advice about `correct' use of decorator
Re: True of False
Re: local function definitios are illegal
Re: Simple python iteration question
Re: Evaluation sequence and precedence
Kidprotect
Nasze Dzieci
Podaruj Zycie
Fundacja Sloneczko
Rodzic Po Ludzku