为什么迭代器需要是默认可构造的 [英] Why do iterators need to be default-constructible

查看:107
本文介绍了为什么迭代器需要是默认可构造的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类别转发双向随机访问的迭代器需要是默认构造的。



为什么,为什么输入输出运算符不必是默认构造的

解决方案

需要转发迭代器和更强的引用一些外部序列(参见[forward.iterators] / 6, a 和 b 都是可解引用的,则 a == b * a * b 绑定到同一个对象。 b
$ b

这意味着它们通常只是一个轻量级的句柄(例如指向一个元素或容器中的一个节点的指针),所以没有什么理由不要求它们可以是默认的构造(即使默认构造创建一个单一的迭代器,不能用于任何东西,直到赋予一个新的值)。所有非病理的*正向迭代器都可以支持默认构造,并且依赖于这一点使得一些算法更容易实现。



迭代器只满足输入迭代器或输出迭代器需求(没有更强的)可能包含由 operator ++ 修改的自身内的状态,因此可能不可能对该状态进行默认构造。没有只对输入/输出迭代器操作的算法需要默认构造它们,因此不是必需的。




  • scotsmanargument here;)


Iterators of the categories forward, bidirectional, and random access need to be default-constructible.

Why is this, and why do input and output operators not have to be default-constructible?

解决方案

Forward iterators and stronger are required to refer to some external sequence (see [forward.iterators]/6 which says "If a and b are both dereferenceable, then a == b if and only if *a and *b are bound to the same object.")

This means they are generally just a lightweight handle onto something else (e.g. a pointer to an element or a node in a container) and so there is little reason not to require that they can be default constructed (even if default construction creates a singular iterator that can't be used for anything until assigned a new value). It's possible for all non-pathological* forward iterators to support default construction, and relying on that makes some algorithms easier to implement.

Iterators which only meet the input iterator or output iterator requirements (and nothing stronger) might contain state within themselves which is modified by operator++ and so it might not be possible for that state to be default-constructed. No algorithm that only operates on input/output iterators needs to default construct them, so it isn't required.

  • spot the "no true scotsman" argument here ;)

这篇关于为什么迭代器需要是默认可构造的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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