C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

about partial specialization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Rex_chaos
Guest





PostPosted: Thu Oct 23, 2003 3:29 pm    Post subject: about partial specialization Reply with quote



Hi there,
I am writing a template class like

template <class T, int rank>
class TC {...};

In my case, I would like to partial specialize the class.

template <class T>
class TC<T,1> {...};

template <class T>
class TC<T,2> {...};

template <class T>
class TC<T,2> {...};

Within each class, there is a little change. A particular function
will be added to the class and the implementation of some function
will also be modified. I wonder it it's necessary to "copy and paste"
all the code from the original template class to the specialized one.
It's very annoying if I have to.

For this purpose, I wonder if it's better to use the inheritance
instead.

template <class T>
class TCOne :public TC<T,1>
{...};

For my case, the efficiency is the most essential factor. Will it
lower the efficiency if I take the inheitance mechanism?

Thanks in advance.
Back to top
Gianni Mariani
Guest





PostPosted: Thu Oct 23, 2003 3:39 pm    Post subject: Re: about partial specialization Reply with quote



Rex_chaos wrote:
....
Quote:
For this purpose, I wonder if it's better to use the inheritance
instead.

template <class T
class TCOne :public TC {...};

For my case, the efficiency is the most essential factor. Will it
lower the efficiency if I take the inheitance mechanism?

No. Inheritance has very low (if no) run time cost.


By the description, I thought you meant to do this.

template class TC<T,1> : public BASE
{...};


Back to top
Grzegorz Sakrejda
Guest





PostPosted: Thu Oct 23, 2003 3:48 pm    Post subject: Re: about partial specialization Reply with quote



On 23 Oct 2003 08:29:54 -0700, Rex_chaos <rex_chaos (AT) 21cn (DOT) com> wrote:

Quote:
Hi there,
I am writing a template class like

template <class T, int rank
class TC {...};

In my case, I would like to partial specialize the class.

template class TC
template <class T
class TC
template <class T
class TC
Within each class, there is a little change. A particular function
will be added to the class and the implementation of some function
will also be modified. I wonder it it's necessary to "copy and paste"
all the code from the original template class to the specialized one.
It's very annoying if I have to.

And it's a maintanance mess.

Quote:

For this purpose, I wonder if it's better to use the inheritance
instead.

Separate repeated code and put it into the base class.

Quote:

template <class T
class TCOne :public TC
For my case, the efficiency is the most essential factor. Will it
lower the efficiency if I take the inheitance mechanism?
I don't think so.


Quote:

Thanks in advance.


You also could use macro to avoid repeating code.
It's ugly but if there is nothing better why not use it.

--
grzegorz

Back to top
stephan beal
Guest





PostPosted: Fri Oct 24, 2003 8:40 am    Post subject: Re: about partial specialization Reply with quote

Gianni Mariani wrote:
Quote:
template <class T
class TCOne :public TC {...};
....
By the description, I thought you meant to do this.

template class TC {...};

No, he means for a specialization to subclass the class template which it is
specializing. Convoluted, perhaps, but i've done this in the past as well.
e.g.,

template <class SerializableType,class NodeType = s11n::s11n_node>
class serializable_adapter
{
....
};

/**
Quasi-bogus specialization to accomodate usage of, e.g.,
container::value_type as a SerializableType parameter.
This feature is used by, e.g., de/serialize_{map,list}() to
strip the pointer part from value_type so it can be re-used
as a template parameter for other functions.
*/
template <class SerializableType,class NodeType>
class serializable_adapter<SerializableType *,NodeType> : public
serializable_adapter <SerializableType,NodeType>{};

So, in end effect this causes pointer and non-pointer SerializableTypes to
be treated identically (at least in the cases i've had so far).

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.


Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group