about partial specialization

R

Rex_chaos

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.
 
G

Gianni Mariani

Rex_chaos wrote:
....
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?

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


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

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

Grzegorz Sakrejda

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.

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

Separate repeated code and put it into the base class.
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?
I don't think so.
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.
 
S

stephan beal

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

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

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top