Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template specialization help

Reply
Thread Tools

Template specialization help

 
 
Adrian
Guest
Posts: n/a
 
      02-19-2011
I am trying to write a specialized version of print that will
automagically work out that the container is a pointer of containers and
does a double ref on each iterator.

It feels possible but I cannot think of a way to do it. My initial
thoughts where to use the STL contains ::value_type and ::const_pointer
typedefs

Happy to modify template params to pass enough information

Anyone done this, or got any ideas on how

Thanks

TIA

Adrian


#include <iostream>
#include <vector>

template<class T>
void print(T first, T last)
{
for(T i=first; i!=last; ++i)
std::cout << (*i) << std::endl;
}

#if 0
template<>
void print(T first, T last)
{
for(T i=first; i!=last; ++i)
std::cout << (**i) << std::endl;
}
#endif
int main(int argc, char *argv[])
{
std::vector<int> intlist;
std::vector<int*> intptrlist;

for(int i=0; i<10; ++i)
{
intlist.push_back(i);
intptrlist.push_back(new int(i));
}

print(intlist.begin(), intlist.end());
print(intptrlist.begin(), intptrlist.end());

return 0;
}
 
Reply With Quote
 
 
 
 
Adrian
Guest
Posts: n/a
 
      02-19-2011
On 2/18/2011 5:50 PM, Adrian wrote:
> container is a pointer of containers

Sorry. Should be
container is a container of pointers
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-19-2011
On 02/19/11 01:50 PM, Adrian wrote:
> I am trying to write a specialized version of print that will
> automagically work out that the container is a pointer of containers and
> does a double ref on each iterator.
>
> It feels possible but I cannot think of a way to do it. My initial
> thoughts where to use the STL contains ::value_type and ::const_pointer
> typedefs
>
> Happy to modify template params to pass enough information
>
> Anyone done this, or got any ideas on how


Um, I'm sure there's a cleaner way, but these work for ordered and
unordered containers:

template <typename T,
template <typename E,
typename A = std::allocator<E> > class Container>
void print( const Container<T>& c )
{
for( typename Container<T>::const_iterator i = c.begin(); i !=
c.end(); ++i )
std::cout << *i << std::endl;
}

template <typename T,
template <typename E,
typename L = std::less<E>,
typename A = std::allocator<E> > class Container>
void print( const Container<T>& c )
{
for( typename Container<T>::const_iterator i = c.begin(); i !=
c.end(); ++i )
std::cout << *i << std::endl;
}

template <typename T,
template <typename E,
typename A = std::allocator<E> > class Container>
void print( const Container<T*>& c )
{
for( typename Container<T*>::const_iterator i = c.begin(); i !=
c.end(); ++i )
std::cout << **i << std::endl;
}

template <typename T,
template <typename E,
typename L = std::less<E>,
typename A = std::allocator<E> > class Container>
void print( const Container<T*>& c )
{
for( typename Container<T*>::const_iterator i = c.begin(); i !=
c.end(); ++i )
std::cout << **i << std::endl;
}

The horrible template template syntax will be much cleaner in C++0x.

> int main(int argc, char *argv[])
> {
> std::vector<int> intlist;
> std::vector<int*> intptrlist;
>
> for(int i=0; i<10; ++i)
> {
> intlist.push_back(i);
> intptrlist.push_back(new int(i));
> }
>
> print(intlist.begin(), intlist.end());
> print(intptrlist.begin(), intptrlist.end());
>
> return 0;
> }


--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
template specialization overriding non-specialization? Joseph Turian C++ 2 04-16-2006 02:46 PM
Template specialization for templated and primitive type template parameters case2005 C++ 3 02-13-2005 06:53 PM
template specialization of a template class pit3k C++ 8 02-09-2005 02:39 AM
Specialization of a template within template David B. Held C++ 2 10-26-2003 11:59 AM



Advertisments