473,835 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

specialization

I have a class

template <class Vector_t>
class A;

where Vector_t is some sort of vector. I want to provide different
specializations for real and for complex vectors. I need the following:

// Real vectors:
A< valarray<double > >; // templated
A< MyRealVector >; // non-tempalted

// Complex vectors:
A< vector< complex<double> >; // templated
A< MyComplexVector >; // non-templated.

Is this possible? How would I do that? Bad design? What's the right way?
Thanks!

Feb 24 '06 #1
3 1317
Amadeus W. M. wrote:
I have a class

template <class Vector_t>
class A;

where Vector_t is some sort of vector. I want to provide different
specializations for real and for complex vectors. I need the following:

// Real vectors:
A< valarray<double > >; // templated
What do you mean by the word 'templated' in the comment?
A< MyRealVector >; // non-tempalted

// Complex vectors:
A< vector< complex<double> >; // templated
A< MyComplexVector >; // non-templated.

Is this possible? How would I do that? Bad design? What's the right way?


Have you tried? What's the result? If you haven't, what's stopping you?
What book are you reading that doesn't tell how to specialise a template?

template<> class A<MyRealVector > { /* specialisation */ };

or

typedef A<MyRealVector > MyRealA;

They give you different results, of course. The former would require you
to actually specify the inner workings (which are supposedly different
from the original template), the latter _uses_ the inner workings of the
original template.

V
--
Please remove capital As from my address when replying by mail
Feb 24 '06 #2
On Fri, 24 Feb 2006 12:31:02 -0500, Victor Bazarov wrote:
Amadeus W. M. wrote:
I have a class

template <class Vector_t>
class A;

where Vector_t is some sort of vector. I want to provide different
specializations for real and for complex vectors. I need the following:

// Real vectors:
A< valarray<double > >; // templated


What do you mean by the word 'templated' in the comment?
A< MyRealVector >; // non-tempalted

// Complex vectors:
A< vector< complex<double> >; // templated
A< MyComplexVector >; // non-templated.


Sorry, I didn't explain very well.
I mean that the template argument of A is itself a template.

So I have the most general

template <class Vector_t>
class A
{
};

I don't want to specialize it to a particular vector type, such as
valarray<double > or MyNonTemplatedV ector. The specializations differ in
the SCALAR type of the vector, but should be irrespective of the actual
container. So what I'd like would be rather

template <class Scalar_t, template<class> class Vector_t>
class A
{
// work on Vector_t<Scalar _t>;
};

Then this should somehow be specialized (1) to Scalar_t = float/double and
(2) to Scalar_t = complex<double> , for instance, but still have the
container Vector_t as a parameter.

I'm reading Stroustrup's book. Can this be done?

Even if it CAN be done, with a template Vector_t I won't be able to have a

A<MyNonTemplate dVector>;
Maybe I should just do

template <class Vector_t>
class RealA
{

};

template <class Vector_t>
class ComplexA
{

};

Then Vector_t is the most general, and I have two implementations - one
for real, one for complex. It won't be transparent to the user though.
Feb 24 '06 #3

Amadeus W. M. wrote:
On Fri, 24 Feb 2006 12:31:02 -0500, Victor Bazarov wrote:
Amadeus W. M. wrote:
I have a class

template <class Vector_t>
class A;

where Vector_t is some sort of vector. I want to provide different
specializations for real and for complex vectors. I need the following:

// Real vectors:
A< valarray<double > >; // templated
What do you mean by the word 'templated' in the comment?
A< MyRealVector >; // non-tempalted

// Complex vectors:
A< vector< complex<double> >; // templated
A< MyComplexVector >; // non-templated.


Sorry, I didn't explain very well.
I mean that the template argument of A is itself a template.

So I have the most general

template <class Vector_t>
class A
{
};

I don't want to specialize it to a particular vector type, such as
valarray<double > or MyNonTemplatedV ector. The specializations differ in
the SCALAR type of the vector, but should be irrespective of the actual
container. So what I'd like would be rather

template <class Scalar_t, template<class> class Vector_t>
class A
{
// work on Vector_t<Scalar _t>;
};

Then this should somehow be specialized (1) to Scalar_t = float/double and
(2) to Scalar_t = complex<double> , for instance, but still have the
container Vector_t as a parameter.

I'm reading Stroustrup's book. Can this be done?


Yes:

template <class Scalar_t, template<class> class Vector_t>
class A
{
// work on Vector_t<Scalar _t>;

};

template < template<class> class Vector_t>
class A<double, Vector_t>
{

};
Even if it CAN be done, with a template Vector_t I won't be able to have a

A<MyNonTemplate dVector>;


You can if you declare another "A" class template, in addition to the
first.

Greg

Feb 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6870
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
4
6495
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
6
2646
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
8
7692
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
4
1969
by: TT \(Tom Tempelaere\) | last post by:
Comeau compiler complains (too few arguments for class template "B") at line *** #include <memory> template<typename T, size_t n> struct A {}; template<typename T, size_t n> struct B;
2
1207
by: Jonathan Turkanis | last post by:
Hi All, The simple test program at the end of this message defines a class template foo with a member function bar implemented by delegating to a static member function of a specialization of the class template bar_impl, nested within foo. The template bar_impl is partially specialized outside of foo. My question is which specialization should be selected: the primary template or the partial specialization. Or does the program exhibit...
19
2983
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $ Last-Modified: $Date: 2003/09/22 04:51:49 $ Author: Nicolas Fleury <nidoizo at gmail.com> Status: Draft Type: Standards Track
2
7706
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
8
2971
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
6
2623
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass Indexer<std::vector<T,A {} matches only with nonconst version. anyway to match it for both? and get if it is const or nonconst? Actually i want 2 specialization, one for std::vector<T,Aconst & non const other for std::deque<T,Aconst & non const.
0
9803
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
9652
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,...
1
10560
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
10233
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9344
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...
1
7766
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3088
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.