• Advertisement
Sign in to follow this  

Template specialization with another template

This topic is 3678 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

hi! How can i specialize my generic Write function
template < typename T > 
void Write( MySerializer& ser, T data)
{
  //do something
}
// int specialization : 
template <> 
void Write<int>( MySerializer& ser, int data)
{
  //write the int
}

with another template //template <typename T> class Array
// syntax error
template < > 
void Write< Array<T> >( MySerializer& ser, Array<T> data)
{
  //for each member of data call Write( ser, data )
}

thanks!

Share this post


Link to post
Share on other sites
Advertisement
AFAIK you can not, but this is not a problem because...


// syntax error
template < typename T >
void Write( MySerializer& ser, Array<T> data)
{
//for each member of data call Write( ser, data )
}




...is not a specialization of your function, but does what you want.
In fact, what is a specialization if not a different function? ;)
Kind regards

[edit]
You might pass the Array by ref or const ref!

Share this post


Link to post
Share on other sites
Or using the idioms of the standard library:


template< class Iterator >
void Write( MySerializer &ser, Iterator begin, Iterator end )
{
while( begin != end )
{
Write(ser,*begin);
++begin;
}
}



You can then do this (assuming Array follows the Standard C++ Library conventions on iterators):

template< class Container >
void Write( MySerializer &ser, const Container &c )
{
Write(ser,c.begin(),c.end());
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement