Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Template specialization

This topic is 5927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I have a question regarding specialization of templates. I have a template template class List { T& front() { return first->value; } // ... } and want to make a specialization of the template for pointers in order to avoid code bloat sinc I use many "lists" of poiters. First I make a specialization for void*: template<> class List { void*& front() { return first->value; } // ... } and then I provide a partial specialization "interface" to the List (with all functions inlined) : template class List : private List { T*& front() { return ( T*& ) first->value; } // ... } Now, the List looks nearly exactly like the List template. Actually, when I constructed the List template I copied the code from the List and only changed to ! So, I write exactly the same code twice ! This is no good because I have to remember to make changes to both List and List. Am I doing something wrong ? Can I avoid writing all the functions in List again when they look exactly (except for -> ) like the ones in List ? Question 2: I have an iterator for the List class, and I have to convert an Iterator to an Iterator. Writing Iterator List::begin() { return (Iterator ) List::begin(); } doesn''t work since there is no constructor Iterator::Iterator( const Iterator& ). So, how do I convert an Iterator to an Iterator ? Right now I do this (but it''s pretty ugly and I don''t know if it works): Iterator List::begin() { return *( (Iterator* ) & List::begin() ); } Thanks for reading this and more thanks if you have some answers !

Share this post

Link to post
Share on other sites
Hello LeoMaheo,

In answer to question 1) the T* specialization should be written in terms of the void* specialization, just casting as appropiate (see below).

Although at the moment the void* specialization does not do anything the over and above basic template (So for the moment, I've just commented it out)

Question 2) I'm not so sure about, so I'll leave it to someone else.



For example


template<class T> class List {
T& front() { return first->value; }
// ...


//template<> class List<void*> {

// void*& front() { return first->value; }

// // ...


template<T*> class List : private List<void*> {
T*& front() { return (T*) List<void*>::front() }
// ...


Standard disclaimer: This is off the top of my head and with no compiler around could be completely wrong.

[edited by - TomH on July 26, 2002 9:52:10 AM]

Share this post

Link to post
Share on other sites
Thanks, that was usefull becase I did another trie now and then everything worked.
Actually I had tried that before, but now I tried again and added "template<> List;" and then everything worked all right.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!