473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

about partial specialization

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.
Jul 19 '05 #1
3 1425
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
{...};

Jul 19 '05 #2
On 23 Oct 2003 08:29:54 -0700, Rex_chaos <re*******@21cn.com> wrote:
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.

--
grzegorz
Jul 19 '05 #3
Gianni Mariani wrote:
template <class T>
class TCOne :public TC<T,1>
{...};
.... 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.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
9
by: Philip Lawatsch | last post by:
Hi I'd like to implement some kind if type traits myself, but I have to support broken compilers (like visual studio) that do not support Partial Specialization. My first shot was something...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
1
by: SainTiss | last post by:
Hi, I've been looking into the standard for a clear statement on whether partial specialization of member functions of class templates is allowed or not. 14.7.3/4 says that explicit...
1
by: BekTek | last post by:
I'm still confused about the template partial specialization which is used in many libraries.. due to lack of introduction for beginner.. Could you tell me about that in short? Thanks in...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
9
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.