473,416 Members | 1,547 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,416 software developers and data experts.

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 2246
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 "regurgitate" 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
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...
9
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...
8
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...
5
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
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
9
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...
12
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...
10
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...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...
0
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...

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.