473,883 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on partial specializatio (class template specialization)

if I have a class template declared as ff:
(BTW is this [below] a partial specialization? - I think it is)

template <typename T1, myenum_1 e1=OK, my_enum_2=NONE>
class A {

public:
A();
virtual ~A() ;

void foo(void);
void bar(const char*) ;
char* foobar(int) ;
// ... lots more methods

private:
T1 *m_obj ;
my_enum1 m_enum1 ;
myenum_2 m_enum2 ;
};
suppose I decide to specialize the above template further as ff:

template<>
class A <MyType, NOT_OK, ALL> {

};

My question are:

1). Do I need to declare (and define) all of the methods as done
previously for the original class template? - I ask this because it
seems terribly ineffecient and error prone - retyping all that code again.

2). If the answer to the above question is yes (i.e. retyping all the
declarations/definitions again), then is there any point at all in
having a generic class (when you know you will have to specialize it?).
It seems a much better design choice would have been to simply use
inheritance in such an instance - is this a valid conclusion to draw?

3). (Assuming that the answer to question 1 is yes) If another
(template) class inherits from my specialized class template A, then I
would (it appears) have to regurgitate code for each of the combinations
of my derived class specializations and the base class specializations !.
It would seem clas template specialization is a very poor design choice
if one knows at the start that one would require specialization of the
class template. Is this a generally true conclusion - or is there some
thing I am missing?
Aug 24 '05 #1
1 2276
Alfonso Morra wrote:
if I have a class template declared as ff:
(BTW is this [below] a partial specialization? - I think it is)
No, it's a template definition. The presence of default argument values
changes nothing.
template <typename T1, myenum_1 e1=OK, my_enum_2=NONE>
class A {

public:
A();
virtual ~A() ;

void foo(void);
void bar(const char*) ;
char* foobar(int) ;
// ... lots more methods

private:
T1 *m_obj ;
my_enum1 m_enum1 ;
myenum_2 m_enum2 ;
};
suppose I decide to specialize the above template further as ff:
The specialisation below is *full*, not partial.

template<>
class A <MyType, NOT_OK, ALL> {

};

My question are:

1). Do I need to declare (and define) all of the methods as done
previously for the original class template? - I ask this because it
seems terribly ineffecient and error prone - retyping all that code again.
Yes, because the specialisation is a whole different thing. For all the
compiler knows, you specialised the template _specifically_ not to define
any members.
2). If the answer to the above question is yes (i.e. retyping all the
declarations/definitions again), then is there any point at all in
having a generic class (when you know you will have to specialize it?).
Yes. Read a good book on templates.
It seems a much better design choice would have been to simply use
inheritance in such an instance - is this a valid conclusion to draw?
No, it's not a valid conclusion to draw. But you still can use
inheritance if you want.
3). (Assuming that the answer to question 1 is yes) If another
(template) class inherits from my specialized class template A, then I
would (it appears) have to regurgitate code for each of the combinations
of my derived class specializations and the base class specializations !.
I don't understand the "regurgitat e" term as applied to template
definitions. Any function defined in the base class is usable in any
derived class unless it's declared 'private' (and the derived class is
not a friend of the base).
It would seem clas template specialization is a very poor design choice
if one knows at the start that one would require specialization of the
class template. Is this a generally true conclusion - or is there some
thing I am missing?


No, it's not generally true. You're missing a whole lot (or so it seems).
Find a good book and study templates. I recommend "C++ Templates" by
Vandevoorde and Josuttis and "Modern C++ Design" by Alexandrescu.

V
Aug 24 '05 #2

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

Similar topics

17
6872
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()).
9
2548
by: Philip Lawatsch | last post by:
Hi I'd like to implement some kind if type traits myself, but I have to support broken compilers (like visual studio) that do not support Partial Specialization. My first shot was something like this: ----8<--- typedef char IsPODForListArrayTrue;
8
7694
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
5
6595
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
4
1860
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
9
2792
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
12
1789
by: Robert.Holic | last post by:
Hi All (first time caller, long time listener), I've stumbled across a problem that I have yet to figure out, although Im sure I'll kick myself when I figure it out. Here it is: I need to create a templated class that accepts any type. Easy enough I though, like this: template<typename Type>
10
2802
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid A<T,3>::f (T) {
1
2232
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
9944
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
9793
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
10858
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
9582
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
7974
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
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.