Template instantiation help needed

From: Jim West (eggplantparts_at_yahoo.com)
Date: 11/20/03


Date: 20 Nov 2003 02:56:23 GMT


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



Relevant Pages

  • Re: DSP - General Question
    ... Suppose one has a good FFt program, and took the FFt of 5 real data points ... Adding 5 0's to the 5 complex data from above, and taking the inverse, will ... and trying to find a convolution equation (to ...
    (comp.dsp)
  • Re: If your ifft slower than your fft?
    ... Probably because you are taking the FFT of real data, but then taking the IFFT of complex data. ... Complex arithmetic should take at least twice as long due to the need to deal with the imaginary part. ...
    (comp.soft-sys.matlab)
  • Re: DSP - General Question
    ... Suppose one has a good FFt program, and took the FFt of 5 real data points ... Adding 5 0's to the 5 complex data from above, and taking the inverse, will ... take the inverse FFt and arrive at the original 5 real data points? ...
    (comp.dsp)