模板类作为模板参数

时间:2014-12-06 20:26:10

标签: c++ templates

如何在容器和类型上定义模板功能?

例如,重载插入运算符以流式传递矢量,列表或转发迭代器容器的所有元素:

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;
}

这不能在g ++ 4.9上编译。怎么了?怎么做?

3 个答案:

答案 0 :(得分:1)

std::vector是一个包含两个模板类型参数的类模板:

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

要使您的函数与std::vector(和其他双参数类模板)一起使用,您可以使用以下定义:

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;
}

或者:

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

答案 1 :(得分:1)

为什么不直接将容器类型作为模板参数传递,并从中找出元素类型?在您的示例代码中,您甚至不需要元素类型:

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;
}

(虽然在没有enable_if技巧的情况下将此用于此类全局函数可能是不明智的,因为它将匹配任何参数。)

编辑:您可以尝试将此限制为具有嵌套value_type的类(所有容器都有):

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

答案 2 :(得分:0)

Alan Stokes的方法很有效。下面的代码可以流式传输任何容器。我只需要为地图添加插入操作符

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;
}