Sign in to follow this  
benj

Template specialization with another template

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[i] )
}

thanks!

Share this post


Link to post
Share on other sites
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[i] )
}




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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this