【问题标题】:What is the correct way to implement iterator and const_iterator in C++17?在 C++17 中实现迭代器和 const_iterator 的正确方法是什么?
【发布时间】:2020-02-27 20:49:03
【问题描述】:

在实现自定义容器时,我需要实现迭代器。当然,我不想为 const 和非 const 迭代器编写两次代码。我发现this question 详细说明了这样的可能实现:

template<class T>
class ContainerIterator {
    using pointer = T*;
    using reference = T&;
    ...
};

template<class T>
class Container {
    using iterator_type = ContainerIterator<T>;
    using const_iterator_type = ContainerIterator<const T>;
}

但我也发现了这个使用模板参数的this question

template<class T, bool IsConst>
class ContainerIterator {
    using pointer = std::conditional_t<IsConst, const T*, T*>;
    using reference = std::conditional_t<IsConst, const T&, T&>;
    ...
};

template<class T>
class Container {
    using iterator_type = ContainerIterator<T, false>;
    using const_iterator_type = ContainerIterator<T, true>;
}

第一个解决方案似乎更容易,但答案来自 2010 年。 在做了一些研究之后,似乎第一个版本没有被广泛使用,但我不明白为什么。我觉得我错过了第一个版本的一些明显缺陷。


所以问题变成了:

  1. 第一个版本有问题吗?

  2. 如果不是,为什么版本 #2 似乎是 c++17 中的首选方式?或者为什么我应该更喜欢一个?


另外,是的,使用const_cast 或简单地复制整个代码将是一种解决方案。但我不喜欢这两个。

【问题讨论】:

  • 不是具体问题的答案,但请注意 boost::iterator_facadeboost::iterator_adaptor 有助于创建迭代器类型所需的大量样板。
  • 另外,除了“在 C++17 中”,这个问题怎么不是你链接到的那个的重复?
  • @NicolBolas 感谢您的澄清,我编辑了我的代码以反映这一点。我实际上是从std::array::iterator 的实现中得到这个的,并认为它会像这样命名,只在最后出现在智能感知中。作为一种“几乎隐藏”的类之类的。
  • @NicolBolas 这个问题更多地针对原因,为什么应该首选第二种实现(应该吗?)。因为我看不到,为什么第一个解决方案在 c++17 中会变得无效或不切实际。

标签: c++ c++17


【解决方案1】:

第二个实现的重点是你没有复制的东西:使实现特定需求变得容易。即,iterator 必须隐式转换为 const_iterator

这里的困难在于保持微不足道的可复制性。如果您要这样做:

template<class T>
class ContainerIterator {
    using pointer = T*;
    using reference = T&;
    ...
    ContainerIterator(const ContainerIterator &) = default; //Should be trivially copyable.
    ContainerIterator(const ContainerIterator<const T> &) {...}
};

这行不通。 const const Typename 解析为 const Typename。因此,如果您使用const T 实例化ContainerIterator,那么您现在有两个具有相同签名的构造函数,其中一个是默认的。好吧,这意味着编译器将忽略您对复制构造函数的默认设置,从而使用您不平凡的复制构造函数实现。

这很糟糕。

有一些方法可以通过使用一些元编程工具来检测T 的 const-ness 来避免这种情况,但修复它的最简单方法是将 const-ness 指定为模板参数:

template<class T, bool IsConst>
class ContainerIterator {
    using pointer = std::conditional_t<IsConst, const T*, T*>;
    using reference = std::conditional_t<IsConst, const T&, T&>;
    ...

    ContainerIterator(const ContainerIterator &) = default; //Should be trivially copyable.
    template<bool was_const = IsConst, class = std::enable_if_t<IsConst || !was_const>>>
    ContainerIterator(const ContainerIterator<T, was_const> &) {...}
};

模板永远不会被认为是复制构造函数,因此这不会影响微不足道的可复制性。如果它不是const 迭代器,这也使用 SFINAE 来消除转换构造函数。

More information about this pattern can be found as well.

【讨论】:

  • 为什么迭代器应该是微不足道的可复制的?在关于迭代器的 cppreference 页面 (en.cppreference.com/w/cpp/named_req/BidirectionalIterator) 上,我没有找到这样的命名要求。
  • @wcobalt:如果可以,为什么不应该可以轻松复制?是的,没有明确的要求,但这并不意味着你也应该放弃这种可能性。尤其是在提供它非常简单的时候。
  • 对不起,我不明白你需要什么 was_const。你能解释一下吗?
  • @wcobalt:我不知道was_const有没有需要;我只是从链接中复制代码。我对 SFINAE 的了解和理解是有限的,所以我只是一般假设如果有模板参数,那么它需要是出于某种原因。
  • SFINAE 仅适用于具有函数自己的模板参数的成员函数,而不是其类的模板参数。这就是为什么需要一个附加参数的原因,尽管它在您的代码中不起作用,因为它实际上并没有在enable_if 中使用。但是,在您链接的博客中,WasConst 是从函数参数推导出来的,并且被使用是因为显然通常的技巧不适用于非 GCC 编译器(对我来说是新闻——也许它只是旧的编译器)。跨度>
猜你喜欢
  • 2016-08-31
  • 1970-01-01
  • 2011-12-07
  • 2011-02-23
  • 2011-07-17
  • 1970-01-01
  • 2015-09-10
  • 2011-11-06
  • 2018-08-12
相关资源
最近更新 更多