如何使我的迭代器类看起来不像容器类?

Emi*_*erg 9 c++ templates googletest c++11

前提

假设我有一个容器类Box,它提供内部类const_iteratoriterator.因为我想要一个iterator可以转换为a const_iterator,后者继承自前者:

class Box {
  // ...
public:
  class const_iterator : public std::iterator<std::random_access_iterator_tag, const int> { /* ... */ };
  class iterator : public const_iterator { /* ... */ };
  // ...
};
Run Code Online (Sandbox Code Playgroud)

问题

现在我想使用Google Test测试这些类.让我们断言,begin()并且end()不要返回相同的东西:

const Box a;
EXPECT_NE(a.begin(), a.end());
Run Code Online (Sandbox Code Playgroud)

跟编译错误打招呼:

  • 铛: no member named 'begin' in 'Box::const_iterator'
  • 克++: ‘const class Box::const_iterator’ has no member named ‘begin’

原因

一些研究让我在Google Test源代码中找到了这个模板(按照扩展文档的链接):

typedef int IsContainer;
template <class C>
IsContainer IsContainerTest(int /* dummy */,
                            typename C::iterator* /* it */ = NULL,
                            typename C::const_iterator* /* const_it */ = NULL) {
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个模板魔术的结果是,如果EXPECT_*iteratorconst_iterator成员类的参数,那么该类型被假定为容器类.知道了这一点,Google Test可以在预期失败时打印漂亮的人工可读报告,这很不错.

但是,有一些细节:

// Note that we look for both C::iterator and C::const_iterator.  The
// reason is that C++ injects the name of a class as a member of the
// class itself (e.g. you can refer to class iterator as either
// 'iterator' or 'iterator::iterator').  If we look for C::iterator
// only, for example, we would mistakenly think that a class named
// iterator is an STL container.
Run Code Online (Sandbox Code Playgroud)

所以,如果我理解正确,这意味着

  • Box::const_iterator将自己作为一个名为的成员类const_iterator,并std::iterator作为一个名为的成员类iterator.
  • Box::iterator将自己作为一个名为的成员类iteratorBox::const_iterator一个名为的成员类const_iterator.

因此,我的迭代器类看起来都像是Google Test的容器类!

这个问题

如何设计我的迭代器类以使它们看起来不像容器?

我试过的事情:

  • 声明as 的std::iterator超类.这解决了隐藏成员类的问题,但它仍然不允许我作为参数传递,除非是.似乎Google Test使用而不是出于某些原因.const_iteratorprivateconst_iteratoriteratora.begin()EXPECT_NEaconstiterator begin()const_iterator begin() const
  • std::iterator完全删除超类.这是一个坏主意吗?我想我必须std::iterator_traits手动申报,还有什么我不会因为没有延期而失去std::iterator
  • 声明as 的Box::const_iterator超类.这可能是也可能不是一种选择,因为我必须重新声明我想要重用的方法(例如).Box::iteratorprivateoperator++

还有什么我忽略的吗?


这个例子

#include<iterator>
#include <memory> //unique_ptr<T>
#include <gtest/gtest.h>

class ThreeInts {
  std::unique_ptr<int[]> v;

  public:
  ThreeInts() : v(new int[3]) { v[0] = 0; v[1] = 1; v[2] = 2; };
  ThreeInts(int val) : ThreeInts() { v[0] = val; v[1] = val; v[2] = val; };

  bool operator==(const ThreeInts& other) const {
    return v[0] == other.v[0] && v[1] == other.v[1] && v[2] == other.v[2];
  }

  class const_iterator : public std::iterator<std::random_access_iterator_tag, const int> {
  protected:
    int* p;
  public:
    explicit const_iterator(int* p) : p(p) {}
    const_iterator& operator++() { ++p; return *this; }
    bool operator==(const const_iterator& rhs) const { return p == rhs.p; }
    bool operator!=(const const_iterator& rhs) const { return p != rhs.p; }
    int operator*() const { return *p; }
  };

  class iterator : public const_iterator {
  public:
    explicit iterator(int* p) : const_iterator(p) {}
    int& operator*() const { return *p; }
  };

  iterator begin() { return iterator(v.get()); }
  iterator end() { return iterator(v.get()+3); }
  const_iterator begin() const { return const_iterator(v.get()); }
  const_iterator end() const { return const_iterator(v.get()+3); }
};

TEST(ThreeInts, ThisTestCompilesAndPrettyFailureMessagesAreShown) {
  const ThreeInts a(1), b(2);
  ThreeInts c(1), d(2);
  EXPECT_EQ(a, b);
  EXPECT_EQ(a, c);
  EXPECT_EQ(c, d);
}

TEST(ThreeInts, ThisTestCompilesIfTheStdIteratorParentIsPrivate) {
  const ThreeInts a;
  EXPECT_NE(a.begin(), a.end());
}

TEST(ThreeInts, ThisTestAlsoCompilesIfTheStdIteratorParentIsPrivateButItIsAHassle) {
  ThreeInts a;
  ThreeInts::const_iterator beg = a.begin();
  ThreeInts::const_iterator end = a.end();
  //EXPECT_NE(beg, end); // Compile error unless the std::iterator superclass is private
}

TEST(ThreeInts, ThisTestDoesNotCompileEvenIfTheStdIteratorParentIsPrivate) {
  ThreeInts a;
  //EXPECT_NE(a.begin(), a.end());
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)

tml*_*len 1

ThreeInts::iterator不应该继承自ThreeInts::const_iterator,而是应该单独实现。

class ThreeInts::iterator : public std::iterator< std::random_access_iterator_tag, int> { ... }
class ThreeInts::const_iterator : public std::iterator< std::random_access_iterator_tag, const int> { ... }
Run Code Online (Sandbox Code Playgroud)

问题似乎是否则两者都有名为和 的ThreeInts::const_iterator成员(也称为构造函数)。另外,继承不是常量正确的,因为应该只保存一个指针/类似于数据。STL 容器还将两个迭代器分开。const_iteratoriteratoriteratorconst_iteratorconst_iteratorconst

在该代码中,不需要定义迭代器类,只需定义

using iterator = int*;
using const_iterator = const int*;
Run Code Online (Sandbox Code Playgroud)