By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

question on partial specializatio (class template specialization)

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.