繁体   English   中英

C ++中的模板和高阶类

[英]Templates and higher-order kinds in C++

我正在尝试编写一个函数,它接受两个相同包含类型的容器,例如,两个std::vector<int> s,或者std::list<int>std::vector<int> (但不是std::vector<int>std::vector<double> !)

由于我不太确定应该怎么做,所以我决定先写一个测试程序:

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

struct vector_wrapper
{
  template <typename T>
  struct instance_wrapper
  {
    typedef typename std::vector<T> instance;
  };
};

struct list_wrapper
{
  template <typename T>
  struct instance_wrapper
  {
    typedef typename std::list<T> instance;
  };
};

template <typename T, typename C1, typename C2>
void move(typename C1::instance_wrapper<T>::instance& c1, typename C2::instance_wrapper<T>::instance& c2) // line 29
{
  while (c1.size() > 0)
  {
    c2.push_front(c1.back());
    c1.pop_back();
  }
}

int main()
{
  std::vector<int> v;
  std::list  <int> l;

  v.reserve(10);
  for (int i = 0; i < 10; ++i)
    v.push_back(i);

  move<int, vector_wrapper, list_wrapper>(v, l);

  std::for_each(l.begin(), l.end(),
    [] (int i) { std::cout << i << " "; }
  );

  std::cout << std::endl;

  return 0;
}

这段代码使用-std=c++11标志给出了g ++ 4.7的以下编译时错误:

metaclass.cpp:29:24: error: non-template 'instance_wrapper' used as template
... more ...

为什么编译器没有正确识别instance_wrapper作为模板?

编译器已经告诉你出了什么问题(来自ideone的错误):

prog.cpp:25:24:错误:非模板'instance_wrapper'用作模板
prog.cpp:25:24:注意:使用'C1 :: template instance_wrapper'来表示它是一个模板

使用C1::template instance_wrapper而不是C1::instance_wrapper - 同样地,对C2::instance_wrapper执行相同操作:

template <typename T, typename C1, typename C2>
void move(typename C1::template instance_wrapper<T>::instance& c1, 
    typename C2::template instance_wrapper<T>::instance& c2)
{
    // ...

这是因为C1是模板,编译器无法推断出instance_wrapper是模板并将其视为非模板类​​型。

请,请阅读所有编译器输出。 不仅仅是逐行。 通常编译器会说出前一行或后续行中的错误,就像在这种情况下,当它已经给你答案时!

这是一个更好的解决方案,不需要任何奇怪的包装器来禁用参数推断并让客户端指定它们,只是干净简单的SFINAE在C ++ 03中工作:

#include <type_traits> // or Boost equivalent

// a little helper struct to keep the 
// function as clutter-free as possible
template<class C1, class C2, class T = void>
struct enable_if_same_value_type
  : std::enable_if<std::is_same<typename C1::value_type,
        typename C2::value_type>::value, T>
{
};

template<class C1, class C2>
typename enable_if_same_value_type<C1, C2>::type
move(C1& source, C2& target){
  /* ... */
}

请注意,你的代码不是很通用,顺便说一句,因为std::vector不支持push_front ,所以你永远无法传递其中的两个。 另外,我不会调用函数move ,因为你在第二个容器上添加了第一个容器的内容。

也就是说,C ++ 11中有一个std::move重载,需要三个迭代器并将第一个标记的内容移动到作为第三个参数传递的输出迭代器中。 因此,您的main可以重写如下:

#include <algorithm> // algorithmic move
#include <vector>
#include <list>
#include <iterator> // front_inserter
#include <iostream>

int main(){
  std::vector<int> v;
  std::list<int> l;

  v.reserve(10);
  for (unsigned i = 0; i < 10; ++i)
    v.push_back(i);

  // I used 'rbegin'  and 'rend' so the order stays the same
  std::move(v.rbegin(), v.rend(), std::front_inserter(l));

  std::copy(l.begin(), l.end(), std::ostream_iterator<int>(std::cout, " "));
}

Ideone上的实例。

为了将来参考,您可以更简单地执行此操作:

#include <type_traits>

template<typename T, typename A1, typename A2, template <typename, typename> class Cont1, template<typename, typename> class Cont2>
void move(Cont1<T, A1>& from, Cont2<T, A2>& to) {
    while (!from.empty()) { // use !empty(), not size()
        to.push_front(from.back());
        from.pop_back();
    }
}

std::vector<int> v;
std::list<int> l;

move(v, l); // and no need to specify types

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM