准备废弃 std::iterator

Preparation for std::iterator Being Deprecated(准备废弃 std::iterator)
本文介绍了准备废弃 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:

  1. 迭代器样板文件现在需要包含所有必需的 typedefs
  2. 使用迭代器的算法现在需要使用 auto 而不是依赖迭代器来声明类型
  3. Loki Astari 建议 std::iterator_traits 可以更新以工作,而无需从 std:: 继承迭代器
  1. An iterator boilerplate will now need to include all required typedefs
  2. Algorithms working with iterators will now need to use auto rather than depending upon the iterator to declare types
  3. Loki Astari has suggested that std::iterator_traits may be updated to work without inheriting from std::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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)