取消引用分配给双倍增量的OutputIterator [英] Dereference-assignment to a doubly incremented OutputIterator

查看:147
本文介绍了取消引用分配给双倍增量的OutputIterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据(优秀)问题 C ++ OutputIterator增量后要求,我们观察对于可解除引用且可递增的值 r of OutputIterator type X 和值 o 适当的类型,表达式

Per the (excellent) question C++ OutputIterator post-increment requirements, we observe that for a dereferenceable and incrementable value r of OutputIterator type X, and value o of appropriate type, the expression

*r++ = o;

有效且具有与

X a(r);
++r;
*a = o;

但是, a 如果 r 在中间期间增加了多次,则取消引用 - 可分配;也就是说,这段代码有效吗?

However, is it still the case the a is dereference-assignable if r has been incremented more than once in the intervening period; that is, is this code valid?

X a(r);
++r;
++r;
*a = o;

很难看出某个值的操作如何影响另一个值的操作有效性但是,例如 InputIterator (24.2.3)在 ++ r的后置条件下

It's difficult to see how operations on a value can have an effect on the validity of operations on another value, but e.g. InputIterator (24.2.3) has, under the postconditions of ++r:



先前值 r 的任何副本都不需要

dereferenceable或
==

Any copies of the previous value of r are no longer required either to be dereferenceable or to be in the domain of ==.

相关章节: 24.2.2迭代器 24.2.4输出迭代器 17.6.3.1模板参数要求

此外,如果要求有效,是否有任何情况下利用其无效性将有助于实施(wrt效率,简单性) OutputIterator 类型,同时仍然遵守现有要求?

Also, if this is not required to be valid, are there any situations where exploiting its non-validity would aid in the implementation (w.r.t. efficiency, simplicity) of an OutputIterator type while still observing the existing requirements?

推荐答案

该问题于2004年提出,因为缺陷485 ,以及 n3066 澄清了这个问题,要求输出迭代器只需要支持交替增量和解除引用/分配的顺序。因此,在您的示例中, r 在第一个 ++ r 之后无需递增,除非存在干预取消引用/分配。 SGI的 STL 也需要此行为(请参见脚注3)。如上所述,n3225没有修复n3066,所以缺陷2035 被提出;但唉,修复版没有进入C ++ 11的发布版本(ISO / IEC 14882:2011)。

This issue was raised in 2004 as defect 485, and the wording in n3066 clarifies the issue, requiring that an output iterator need only support a sequence of alternating increments and dereference/assignments. So in your example, r need not be incrementable after the first ++r, unless there is an intervening dereference/assignment. This behavior is also required by SGI's STL (see footnote 3). As you mentioned above, n3225 appeared without the fixes from n3066, so defect 2035 was raised; but alas the fix did not make it into the published version of C++11 (ISO/IEC 14882:2011).

此外,缺陷2035表示 a (来自 X a(r ++); )不能像 * a = 0 一样使用:

Furthermore, defect 2035 says that a (from X a(r++);) cannot be used like *a = 0:


在此操作之后[ie, ++ r ] r 不需要是可递增的,并且任何副本都是之前的 r 值不再需要可解除引用或可递增。

"After this operation [i.e., ++r] r is not required to be incrementable and any copies of the previous value of r are no longer required to be dereferenceable or incrementable."

在某些情况下,这可能有助于实施(就简单性而言):参见例如在 ostream_iterator 此问题,其中此类(无效)双增量为忽略只需返回 * this ;只有取消引用/赋值会导致 ostream_iterator 实际增加。

There are situations where this may aid the implementation (in terms of simplicity): see e.g. this question on ostream_iterator, where such (invalid) double increments are ignored simply returning *this; only a dereference/assignment causes the ostream_iterator to actually increment.

这篇关于取消引用分配给双倍增量的OutputIterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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