469,287 Members | 2,436 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

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 2045
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Paul MG | last post: by
9 posts views Thread by Philip Lawatsch | last post: by
8 posts views Thread by Agent Mulder | last post: by
12 posts views Thread by Robert.Holic | last post: by
10 posts views Thread by jason.cipriani | last post: by
1 post views Thread by Ioannis Gyftos | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.