我是否可以获得模板类型的“迭代器”,无论该类型是数组还是类似STL的容器?

时间:2012-02-22 20:34:03

标签: c++ templates iterator c++11

这是我的例子:

template<typename TContainer>
class MyClass
{
public:
   typedef typename SomeUnknownHelper<TContainer>::iterator iterator;
};

std::vector<int>::iterator i = MyClass<std::vector<int>>::iterator;
int *pi = MyClass<int[20]>::iterator;

基本上,我不知道怎么写SomeUnknownHelper

我知道我可以专注MyClass本身,但在我的实际案例中,这将是一个麻烦,因为班级很大。

2 个答案:

答案 0 :(得分:2)

decltypestd::begin

很容易实现
#include <iterator>
#include <utility>

namespace tricks{
  using std::begin; // fallback for ADL
  template<class C>
  auto adl_begin(C& c) -> decltype(begin(c)); // undefined, not needed
  template<class C>
  auto adl_begin(C const& c) -> decltype(begin(c)); // undefined, not needed
}

template<typename TContainer>
class MyClass
{
public:
   typedef decltype(tricks::adl_begin(std::declval<TContainer>())) iterator;
};

std::vector<int>::iterator i = MyClass<std::vector<int>>::iterator;
int *pi = MyClass<int[20]>::iterator;

更好的选择可能是使用Boost.Range:

#include <boost/range/metafunctions.hpp>

template<typename TContainer>
class MyClass
{
public:
   typedef typename boost::range_iterator<TContainer>::type iterator;
};

std::vector<int>::iterator i = MyClass<std::vector<int>>::iterator;
int *pi = MyClass<int[20]>::iterator;

答案 1 :(得分:1)

这只是一个单一的专业化,这有多糟糕?

template <typename T> struct ContainerTrait
{
    typedef typename T::iterator iterator;
    typedef typename T::const_iterator const_iterator;
};

template <typename T, unsigned int N> struct ContainerTrait<T[N]>
{
    typedef T * iterator;
    typedef T const * const_iterator;
};

或者,您可以使用免费的std::begin / std::endauto

auto it = std::begin(x);  // x could be vector<int> or float[10]...