在 C++17 中实现迭代器和const_iterator的正确方法是什么?
What is the correct way to implement iterator and const_iterator in C++17?
在实现自定义容器时,我到了需要实现迭代器的地步。当然,我不想为常量和非常量迭代器编写两次代码。我发现这个问题详细说明了可能的实现,如下所示:
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>;
}
但我也发现了这个问题,它使用模板参数:
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 年开始的。 经过一些研究,似乎第一个版本没有被广泛使用,但我看不出为什么。 我觉得我错过了第一个版本的一些明显缺陷。
所以问题变成了:
第一个版本有什么问题吗?
如果不是,为什么版本 #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
的常量来避免这种情况,但修复它的最简单方法是将常量指定为模板参数:
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> &) {...}
};
模板永远不会被视为复制构造函数,因此这不会干扰简单的可复制性。这也使用 SFINAE 在转换构造函数不是const
迭代器的情况下消除转换构造函数。
还可以找到有关此模式的更多信息。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 通过JNI传递数据数组的最快方法是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 在C++中包含原型文件的正确方法是什么?
- 在 OpenCV C++ 中估计基本矩阵之前对相应点进行归一化的正确方法是什么?
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?
- 将一系列整数放入类的最佳方法是什么?
- 从长整整转换为uint64_t的推荐方法是什么?
- 将此布尔值传递给此函数的最有效方法是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?