Preparation for std::iterator Being Deprecated(准备废弃 std::iterator)
问题描述
3 月 21 日st,标准委员会投票批准了弃用 std::iterator 在 P0174:
On March 21st the standards committee voted to approve the deprecation of std::iterator proposed in P0174:
与简单地在类定义本身中提供预期的 typedef 相比,长长的 void 参数序列对读者来说不太清楚,这是当前工作草案所采用的方法,遵循模式设置在 c++14一个>
The long sequence of void arguments is much less clear to the reader than simply providing the expected
typedefs in the class definition itself, which is the approach taken by the current working draft, following the pattern set in c++14
之前 c++17鼓励从 std::iterator 继承 以消除迭代器样板实现中的乏味.但弃用将需要以下条件之一:
Before c++17 inheritance from std::iterator was encouraged to remove the tedium from iterator boilerplate implementation. But the deprecation will require one of these things:
- 迭代器样板文件现在需要包含所有必需的
typedefs - 使用迭代器的算法现在需要使用
auto而不是依赖迭代器来声明类型 - Loki Astari 建议
std::iterator_traits可以更新以工作,而无需从std:: 继承迭代器
- An iterator boilerplate will now need to include all required
typedefs - Algorithms working with iterators will now need to use
autorather than depending upon the iterator to declare types - Loki Astari has suggested that
std::iterator_traitsmay be updated to work without inheriting fromstd::iterator
在我设计自定义迭代器时着眼于 c++17 兼容性?
Can someone enlighten me on which of these options I should expect, as I design custom iterators with an eye towards c++17 compatibility?
推荐答案
讨论的替代方案很清楚,但我觉得需要一个代码示例.
The discussed alternatives are clear but I feel that a code example is needed.
鉴于不会有语言替代品并且不依赖于 boost 或您自己版本的迭代器基类,以下使用 std::iterator 的代码将固定到下面的代码中.
Given that there will not be a language substitute and without relying on boost or on your own version of iterator base class, the following code that uses std::iterator will be fixed to the code underneath.
template<long FROM, long TO>
class Range {
public:
// member typedefs provided through inheriting from std::iterator
class iterator: public std::iterator<
std::forward_iterator_tag, // iterator_category
long, // value_type
long, // difference_type
const long*, // pointer
const long& // reference
> {
long num = FROM;
public:
iterator(long _num = 0) : num(_num) {}
iterator& operator++() {num = TO >= FROM ? num + 1: num - 1; return *this;}
iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}
bool operator==(iterator other) const {return num == other.num;}
bool operator!=(iterator other) const {return !(*this == other);}
long operator*() {return num;}
};
iterator begin() {return FROM;}
iterator end() {return TO >= FROM? TO+1 : TO-1;}
};
(代码来自 http://en.cppreference.com/w/cpp/iterator/iterator 已获得原作者许可).
(Code from http://en.cppreference.com/w/cpp/iterator/iterator with original author's permission).
template<long FROM, long TO>
class Range {
public:
class iterator {
long num = FROM;
public:
iterator(long _num = 0) : num(_num) {}
iterator& operator++() {num = TO >= FROM ? num + 1: num - 1; return *this;}
iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}
bool operator==(iterator other) const {return num == other.num;}
bool operator!=(iterator other) const {return !(*this == other);}
long operator*() {return num;}
// iterator traits
using difference_type = long;
using value_type = long;
using pointer = const long*;
using reference = const long&;
using iterator_category = std::forward_iterator_tag;
};
iterator begin() {return FROM;}
iterator end() {return TO >= FROM? TO+1 : TO-1;}
};
这篇关于准备废弃 std::iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:准备废弃 std::iterator
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- XML Schema 到 C++ 类 2022-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 将 hdc 内容复制到位图 2022-09-04
- 如何提取 __VA_ARGS__? 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- GDB 不显示函数名 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
