为无效的迭代器定义了哪些操作? [英] Which operations are defined for invalid iterators?

查看:150
本文介绍了为无效的迭代器定义了哪些操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为对

As a follow-up on a question concerning comparing invalid iterators, I tried to find a definition of the allowed expressions for invalid iterators in the C++ standard. Searching for "invalid iterator" finds only a single reference in §24.2.1.11. It says that invalid iterators may be "singular", but only states that dereferencing them may be undefined behavior. No further semantics is given.

其中一个原始答案表明这是实现定义的行为,但是我认为这通常不能被假定,因为上述段落明确地指的是UB.

One of the original answers suggests that it is implementation-defined behavior, but I think that this cannot be assumed in general because the above mentioned paragraph explicitly refers to UB.

此答案显示任何其他使用无效指针值的行为都具有实现定义的行为".由于向量的迭代器通常被实现为指针,因此我认为比较两个无效的迭代器至少是实现定义的行为.

This answer shows that "Any other use of an invalid pointer value has implementation-defined behavior". Since iterators for vectors are often implemented as pointers, I would argue that comparing two invalid iterators is at least implementation-defined behavior.

有人可以指出标准中有关无效迭代器的语义的相关部分吗?

Could anybody point me to the relevant sections in the standard where the semantics for invalid iterators are defined?

推荐答案

也许[iterator.requirements.general,24.2.1]/6包含您所追求的?

Maybe [iterator.requirements.general, 24.2.1]/6 contains what you're after?

大多数表达式的结果都没有为奇异值定义; 唯一的例外是销毁持有奇异值的迭代器,分配非奇异值 value到拥有奇异值的迭代器,对于满足DefaultConstructible的迭代器 要求,使用值初始化的迭代器作为复制或移动操作的源. [注意:此 默认区别不提供保证,尽管区别仅适用于琐碎的类型 默认构造函数,例如指针或包含指针的聚合. —尾注]在这些情况下,单数 值与任何其他值相同的方式被覆盖.可引用的值总是非奇数.

Results of most expressions are undefined for singular values; the only exceptions are destroying an iterator that holds a singular value, the assignment of a non-singular value to an iterator that holds a singular value, and, for iterators that satisfy the DefaultConstructible requirements, using a value-initialized iterator as the source of a copy or move operation. [Note: This guarantee is not offered for default initialization, although the distinction only matters for types with trivial default constructors such as pointers or aggregates holding pointers. — end note] In these cases the singular value is overwritten the same way as any other value. Dereferenceable values are always non-singular.

此外,第11段定义了无效的迭代器:

Moreover, invalid iterators are defined by paragraph 11:

无效迭代器是可能是单数的迭代器.

An invalid iterator is an iterator that may be singular.

因此,无效的迭代器至少与奇异的迭代器一样受约束;具体来说,唯一允许的操作是销毁和分配.

Thus, invalid iterators are at least as constrained as singular iterators; specifically, the only permitted operations are destruction and assignment.

这篇关于为无效的迭代器定义了哪些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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