473,748 Members | 9,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<dou ble>, double>, etc., but that would break existing code
and add a possibility for error (FOO<complex<do uble>, 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 1654
Jim West wrote in
news:sl******** *************** ***@jwest.ecen. okstate.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<dou ble>, double>, etc., but that would break existing code
and add a possibility for error (FOO<complex<do uble>, 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
2019
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 instantiation of foo:
7
2478
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
4
1651
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
2011
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 template class: template <class ABC> struct CT : public ABC
12
2619
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
2
2019
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
3
2736
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 template class called "myTemplate", for which there is an instantiation for 'int' type. Now suppose a class in module B binary wants to use this template
8
2141
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 cross-platform scripting engine. When the scripting engine is embedded in an application, a platform-specific support library is linked in.) My first attempt goes here: ---code begin (library)---
2
2606
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 I'm doing something wrong and g++ just doesn't notice, or if the people at Intel are doing something weird... What am I trying to do? I'm trying to implement a template class that inherits from the Blitz++ array library #include <blitz/array.h>...
4
1597
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 try to expose only one function of my class its failing #include <iostream>
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.