为什么reverse_iterator双重定义其嵌套类型? [英] Why does reverse_iterator doubly define its nested types?

查看:161
本文介绍了为什么reverse_iterator双重定义其嵌套类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来迭代器适配器 reverse_iterator 双重定义了大多数嵌套类型。特别是,它继承了 std :: iterator 的公开 iterator_category value_type 引用 。除了 iterator_category value_type ,这些都是显式的 typedef 再次在类定义中。

It appears that the iterator adaptor reverse_iterator doubly defines most of its nested types. In particular, it inherits publicly from std::iterator which exposes iterator_category, value_type, difference_type, pointer and reference. Except for iterator_category and value_type, these are all explicitly typedef'ed again in the class definition.

24.5.1.1
类模板reverse_iterator
[reverse.iterator]
/ p>

24.5.1.1 Class template reverse_iterator [reverse.iterator]

namespace std {
template <class Iterator>
class reverse_iterator : public
     iterator<typename iterator_traits<Iterator>::iterator_category,
     typename iterator_traits<Iterator>::value_type,
     typename iterator_traits<Iterator>::difference_type,
     typename iterator_traits<Iterator>::pointer,
     typename iterator_traits<Iterator>::reference> {
public:
  typedef Iterator                                            iterator_type;
  typedef typename iterator_traits<Iterator>::difference_type difference_type;
  typedef typename iterator_traits<Iterator>::reference       reference;
  typedef typename iterator_traits<Iterator>::pointer         pointer;
  // ... rest of the class
};

问题:为什么会出现重复定义?这只是为了展示的目的,还是有更多的?为什么不重新定义 iterator_category value_type

Question: why the repetitive definition? Is this just for purposes of exposition, or is there more to it? And why not redefine iterator_category and value_type?

推荐答案

一段时间以来,他们已经不再使用 std :: iterator 作为基类,迭代器必须定义正确的类型名。

For a while now, they've been moving away from using std::iterator as a base class, and toward just specifying that each iterator must define the correct type names.

当他们在标准中指定基类时,它限制实现来实现类,即使唯一的真正意图是以指定迭代器需要定义一些名称。特别是,您可以 is_base_of 来确定 std :: iterator std :: reverse_iterator 。不,没有什么多态性,所以这样做很愚蠢和毫无意义,但如果你这样做,当前的标准说必须返回true。

When they specify the base class in the standard, that constrains implementations to implement the class that way, even though the only real intent was to specify that the iterator needs to define some names. In particular, you could is_base_of to determine whether std::iterator is a base class of std::reverse_iterator. No, there's nothing polymorphic, so it's pretty silly and pointless to do so, but if you do so, the current standard says it must return true.

它看起来(对我来说),这或多或少是在从(或多或少意外)移动的过程中的意外中途点,需要使用 std :: iterator 作为基类,并简单地指定必须在 std :: reverse_iterator (当然也包括各种其他迭代器)中定义的名称。

It looks (to me) like this is more or less an accidental halfway-point in the process of moving from (more or less accidentally) requiring the use of std::iterator as a base class, and simply specifying the names that must be defined in std::reverse_iterator (and various other iterators as well, of course).

对于那些关心的人来说,历史包括:

For those who care, the history of this includes:

N3931

Issue 2438

还有关于废弃unary_function和binary_function的相关讨论文章:

There's also so related discussion papers about deprecating unary_function and binary_function:

N3145

N3198

这些提供的原因与 std :: iterator (即,只是在派生类中提供一些typedef),因此删除它们的推理与停止使用 std :: iterator 作为基类。

These were provided for roughly the same reasons as std::iterator (i.e., just to provide some typedefs in a derived class) so the reasoning behind removing them is fairly pertinent to ceasing to use std::iterator as a base class.

这篇关于为什么reverse_iterator双重定义其嵌套类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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