c++ - 在 C++17 中实现 iterator 和 const_iterator 的正确方法是什么?

标签 c++ c++17

在实现自定义容器时,我需要实现迭代器。当然,我不想为 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 或简单地复制整个代码也是一个解决方案。但我不喜欢那两个。

最佳答案

第二个实现的要点是您没有复制的内容:使实现特定需求变得容易。即,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 性来避免这种情况,但解决此问题的最简单方法是将 const 性指定为模板参数:

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 .

关于c++ - 在 C++17 中实现 iterator 和 const_iterator 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58661203/

相关文章:

c++ - initializer_list和move语义

C++17 resumable/await:它也可以与 boost::future 一起使用吗?

c++ Linux编译错误

c++ - Schwarz 计数器和可变参数模板

c++ - 如何避免虚拟继承

c++ - 可以存储在 float 中的最大连续整数的定义常量?

c++ - 是否可以使用结构化绑定(bind)来分配类成员?

c++ - 函数类型参数的模板参数推导

C++ - Bool 到 int 转换错误

用户定义类型的 getter 和 setter 上的 C++(类 CAS)同步