question on partial specializatio (class template specialization)

A

Alfonso Morra

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?
 
V

Victor Bazarov

Alfonso said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top