473,395 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Template instantiation help needed


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
2 1638
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
by: Dave | last post by:
Hello all, Consider this template: template <typename T> void foo(T bar) {...} Here are three ways to instantiate this: 1.
6
by: Thomas Maier-Komor | last post by:
Hi everybody, I am a little bit confused with the syntax of explicit instantiation, and I am not sure that it is possible to do what I want to do. Maybe someone has an idea. Consider a...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
4
by: Pallav singh | last post by:
Hi All, i am getting error during explicit function Instantiation for a class Template if i do explicit Instantiation of class it work and all function symbol i get in object file But if i...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.