准备废弃 std::iterator [英] Preparation for std::iterator Being Deprecated

查看:14
本文介绍了准备废弃 std::iterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

3 月 21 日st,标准委员会投票批准了弃用 std::iteratorP0174:

On March 21st the standards committee voted to approve the deprecation of std::iterator proposed in P0174:

与简单地在类定义本身中提供预期的 typedef 相比,长长的 void 参数序列对读者来说不太清楚,这是当前工作草案所采用的方法,遵循模式设置在

以消除迭代器样板实现中的乏味.但弃用将需要以下条件之一:

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

在我设计自定义迭代器时着眼于 兼容性?

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆