By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

Template instantiation help needed

P: n/a

I have a template class (for numerical processing) that was originally
written for real data that I need to extend to complex data, and I am
running into a problem. The current version (greatly simplified) has
something like

template <class T>
class FOO {
T A;
T B;
};

which works fine with T as any floating point type (float, double, etc.).
With complex data however, I would want A to be type complex<T> and B to
be type T. As the operations are identical for both real and complex
data (B represents the absolute value/magnitude of type of A) and the
actual template is quite large, I want to use the same template for both.
The obvious solution is to rewrite it as

template <class DATA, class ABS_TYPE>
class FOO {
DATA A;
ABS_TYPE B;
}

which can be instantiated with FOO<float, float>,
FOO<complex<double>, double>, etc., but that would break existing code
and add a possibility for error (FOO<complex<double>, float> would be
a disasterous loss of precision). Is there a way to get a template
to recognize automatically that B should be type T when A is instantiated
as either T or complex<T>?
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jim West wrote in
news:sl**************************@jwest.ecen.oksta te.edu:

[snip]

template <class T>
class FOO {
T A;
T B;
};

which works fine with T as any floating point type (float, double,
etc.). With complex data however, I would want A to be type complex<T>
and B to be type T. As the operations are identical for both real and
complex data (B represents the absolute value/magnitude of type of A)
and the actual template is quite large, I want to use the same
template for both. The obvious solution is to rewrite it as

template <class DATA, class ABS_TYPE>
class FOO {
DATA A;
ABS_TYPE B;
}

which can be instantiated with FOO<float, float>,
FOO<complex<double>, double>, etc., but that would break existing code
and add a possibility for error (FOO<complex<double>, float> would be
a disasterous loss of precision). Is there a way to get a template to
recognize automatically that B should be type T when A is instantiated
as either T or complex<T>?


template < typename T >
struct extract_real
{
typedef T type;
};

template < typename T >
struct extract_real< std::complex< T > >
{
typedef T type;
};

template < typename T >
class FOO
{
T A;
typename extract_real< T >::type B;
};

HTH

Rob. -- http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
In article <Xn**********************************@195.129.110. 131>, Rob Williscroft wrote:

<solution to my problem snipped>

Worked perfectly, of course. I never, ever would have come up with that.
Thank you for the help!
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.