Template classes as template parameters

How do I define a template template in container and type?

For example, the overload insert operator for a stream of all elements of a vector, list, or iterator container forwarding:

using namespace std;

#include <iostream>
#include <vector>
#include <list>


//...
//...the second argument is a container template-ed on type T
//...
template <typename T,template <typename U> class C>
ostream&
operator<<
  (ostream& p_os,const C<T>& p_c)
{
  for(typename C<typename T>::const_iterator cit=p_c.begin();cit!=p_c.end();++cit)
  {
    p_os.operator<<(*cit);
  }
  return p_os;
}

int
main
  ()
{
  vector<int> v;
  cout << v << endl;
  list<int> l;
  cout << l << endl;
  return 0;
}

      

This doesn't compile in g ++ 4.9. What's wrong? How it's done?

+3


source to share


3 answers


Why not just pass the container type as a template parameter and learn the element type from it? In your example code, you don't even need the element type:

template <typename C>
ostream&
operator<<
  (ostream& p_os,const C& p_c)
{
  typedef typename C::value_type element_type; // if needed
  for(typename C::const_iterator cit=p_c.begin();cit!=p_c.end();++cit)
  {
    p_os.operator<<(*cit);
  }
  return p_os;
}

      

(Though it might be unwise to use this for globals like this without some trick enable_if

, as it will match any argument otherwise.)



EDIT: You can, for example, try to restrict this to nested classes value_type

(which all containers have):

template <typename C, typename T = typename C::value_type>
ostream&
operator<<
  (ostream& p_os,const C& p_c)

      

+1


source


std::vector

is a class template with two template type parameters:

template <class T, class Alloc = allocator<T> >
class vector;

      

To make your function work with std::vector

(and other two parameter class templates), you can use the following definition:



template <typename T, typename A, template <typename, typename> class C>
//                    ~~~~~~~~~^                      ~~~~~~~^
ostream& operator<<(ostream& p_os, const C<T,A>& p_c) 
//                                          ^^
{
  for(typename C<T,A>::const_iterator cit=p_c.begin();cit!=p_c.end();++cit)
  {
     p_os.operator<<(*cit);
  }
  return p_os;
}

      

or alternatively:

template <typename T, template <typename...> class C>
ostream& operator<<(ostream& p_os, const C<T>& p_c);

      

+1


source


Alan Stokes' work is working. The code below can pass any container. I just needed to add an insert operator for cards

using namespace std;

#include <iostream>

#include <vector>
#include <list>
#include <forward_list>
#include <set>
#include <deque>
#include <array>
#include <map>
#include <unordered_map>

//...
//...needed for map types which are (key,value) pairs.
//...
template <typename K,typename V>
ostream&
operator<<
  (ostream& p_os,const pair<const K,V>& p_v)
{
  std::operator<<(p_os,'(');
  p_os << p_v.first;
  std::operator<<(p_os,',');
  p_os << p_v.second;
  std::operator<<(p_os,')');
  return p_os;
}

template <typename C, typename T = typename C::iterator>
ostream&
operator<<
  (ostream& p_os,const C& p_c)
{
  for(typename C::const_iterator cit=p_c.begin();cit!=p_c.end();++cit)
  {
    typename C::value_type v = *cit;
    p_os << v;
    std::operator<<(p_os,",");
  }
  return p_os;
}

int
main
  ()
{
  vector<int> v;
  for(int i=0;i<4;++i)
  {
    v.push_back(i);
  }
  cout << v << endl;
  list<int> l;
  for(int i=0;i<4;++i)
  {
    l.push_back(i);
  }
  cout << l << endl;
  forward_list<int> fl = {0,1,2,3};
  cout << fl << endl;
  set<int> s;
  for(int i=0;i<4;++i)
  {
    s.insert(i);
  }
  cout << s << endl;
  deque<int> d;
  for(int i=0;i<4;++i)
  {
    d.push_back(i);
  }
  cout << d << endl;
  array<int,4> a = {0,1,2,3};
  cout << a << endl;
  unordered_map<int,int> um;
  for(int i=0;i<4;++i)
  {
    um[i] = i;
  }
  cout << um << endl;
  map<int,int> m;
  for(int i=0;i<4;++i)
  {
    m[i] = i;
  }
  cout << m << endl;
  return 0;
}

      

0


source







All Articles