是否有可能摆脱模板专用化以停止递归

Is it possible to get rid of template specialisation to stop recursion?

本文关键字:递归 专用 有可能 是否      更新时间:2023-10-16

我正在编写自己的容器类,它也提供迭代器。这些迭代器可以被取消引用,然后显示原始容器的子范围,再次可以获得一个迭代器。

目前,我有一个模板迭代器类(使用 boost::iterator_facade(,如果L!=0,则取消引用Collection("范围"(,如果L==0,则取消引用T&(存储元素(。是否可以将两者组合在一个类中,以便需要更少的重复代码?

template<typename T, int L>
class CollectionIter : public boost::iterator_facade<
        CollectionIter<T,L>, // type it selfe
        Collection<T,L-1>, // value type
        boost::random_access_traversal_tag,
        Collection<T,L-1> > // deref. type
{
public:
    CollectionIter(T* ptr, const std::vector<int>& collectionSize_)
        : pointer(ptr), collectionSize(collectionSize_) { }
    T* element() { return pointer; }
private:
    friend class boost::iterator_core_access;
    bool equal(const CollectionIter<T,L>& other) const { return pointer==other.pointer; }
    auto dereference() const { return Collection<T,L-1>(pointer, collectionSize); }
    void increment() { pointer = pointer + stepsize(); }
    void decrement() { pointer = pointer - stepsize(); }
    void advance(size_t i) { pointer = pointer + i*stepsize(); }
    auto distance_to(const CollectionIter<T,L>& other) { return (other.pointer - pointer)/stepsize(); }
    int stepsize() { return collectionSize.at(L); }
    T* pointer;
    const std::vector<int>& collectionSize;
};
/* Groundlevel Collection: deref returns T& */
template<typename T>
class CollectionIter<T,0> : public boost::iterator_facade<
        CollectionIter<T,0>,
        T,
        boost::random_access_traversal_tag >
{
public:
    CollectionIter(T* ptr, const std::vector<int>& collectionSize_)
        : pointer(ptr), collectionSize(collectionSize_) { assert(stepsize()==1); }
    T* element() { return pointer; }
private:
    friend class boost::iterator_core_access;
    bool equal(const CollectionIter<T,0>& other) const { return pointer==other.pointer; }
    T& dereference() const { return *pointer; }
    void increment() { pointer = pointer + stepsize(); }
    void decrement() { pointer = pointer - stepsize(); }
    void advance(size_t i) { pointer = pointer + i*stepsize(); }
    auto distance_to(const CollectionIter<T,0>& other) { return (other.pointer - pointer)/stepsize(); }
    int stepsize() { return collectionSize.at(0); }
    T* pointer;
    const std::vector<int>& collectionSize;
};

我在两个版本的CollectionIter中只看到三个区别:

(1( boost::iterator_facade()继承的类接收不同的参数。您可以按照约翰内斯·绍布的建议std::conditional解决这个问题;类似的东西

   public std::conditional< (L > 0U),
              boost::iterator_facade<
                 CollectionIter<T, L>,
                 Collection<T, L-1U>,
                 boost::random_access_traversal_tag,
                 Collection<T, L-1U> >,
              boost::iterator_facade<
                 CollectionIter<T, 0U>,
                 T,
                 boost::random_access_traversal_tag > >

(2( 构造函数中的assert(stepsize()==1);只存在于地面(L == 0U(版本中。您可以将其修改为

assert( (L > 0U) || (stepsize() == 1) ); 

(3(递归dereference()方法在地面版本中确实不同。我不是SFINAE的专家,但是,如果我没记错的话,您可以按如下方式插入两者

  template <int M = L, typename = std::enable_if_t<(M > 0U)>>
  auto dereference () const
   { return Collection<T, L-1U>(pointer, collectionSize); }
  template <int M = L, typename = std::enable_if_t<(M == 0U)>>
  T & dereference () const
   { return *pointer; }

所以整个班级变成了(对不起:我在std::size_t中更改了L(

template <typename T, std::size_t L>
class CollectionIter :
   public std::conditional< (L > 0U),
              boost::iterator_facade<
                 CollectionIter<T, L>,
                 Collection<T, L-1U>,
                 boost::random_access_traversal_tag,
                 Collection<T, L-1U> >,
              boost::iterator_facade<
                 CollectionIter<T, 0U>,
                 T,
                 boost::random_access_traversal_tag > >
 {
   public:
      CollectionIter (T * ptr, const std::vector<int> & collectionSize_)
         : pointer(ptr), collectionSize(collectionSize_)
          { assert( (L > 0U) || (stepsize() == 1) ); }
      T* element() { return pointer; }
   private:
      friend class boost::iterator_core_access;
      bool equal (const CollectionIter<T, L> & other) const
       { return pointer==other.pointer; }
      template <int M = L, typename = std::enable_if_t<(M > 0U)>>
      auto dereference () const
       { return Collection<T, L-1U>(pointer, collectionSize); }
      template <int M = L, typename = std::enable_if_t<(M == 0U)>>
      T & dereference () const
       { return *pointer; }
      void increment ()
       { pointer = pointer + stepsize(); }
      void decrement()
       { pointer = pointer - stepsize(); }
      void advance (size_t i)
       { pointer = pointer + i*stepsize(); }
      auto distance_to (const CollectionIter<T, L> & other)
       { return (other.pointer - pointer)/stepsize(); }
      int stepsize()
       { return collectionSize.at(L); }
      T * pointer;
      const std::vector<int> & collectionSize;
 };