问题描述
根据(优秀)问题,我们观察对于可解除引用且可递增的值 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
:
相关章节: 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年提出,因为澄清了这个问题,要求输出迭代器只需要支持交替增量和解除引用/分配的顺序。因此,在您的示例中, r
在第一个 ++ r
之后无需递增,除非存在干预取消引用/分配。 SGI的也需要此行为(请参见脚注3)。如上所述,n3225没有修复n3066,所以被提出;但唉,修复版没有进入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
:
在某些情况下,这可能有助于实施(就简单性而言):参见例如在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!