Template specialization with another template

Started by
1 comment, last by rip-off 16 years, 2 months ago
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!
Advertisement
AFAIK you can not, but this is not a problem because...

// syntax errortemplate < 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!
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());}

This topic is closed to new replies.

Advertisement