C++ OutputIterator post-increment requirements

Viewed 719

C++ requires that an OutputIterator type X support expressions of the form r++, where r is an instance of X. This postfix increment must be semantically equivalent to:

(*) { X tmp = r; ++r; return tmp; }

and must return a type that is convertible to X const&. In C++11, see 24.2.4 (this is not new, however). In the same section, it says

Algorithms on output iterators should never attempt to pass through the same iterator twice. They should be single pass algorithms.

Given (*), above, say I copy the return value like X a(r++);

  1. Suppose r was dereferencable before incrementing, but was not dereferenced. Is it required that a be dereferencable? If so, must X a(r++); *a = t; perform the same assignment as *r++ = t; would have otherwise? Are there any (other) conditions on a and r ?

  2. Otherwise, suppose r was dereferenced/assigned before incrementing, and its incremented value is (also) dereferencable. Which of the following (if any) are well-defined: (a) *a = t;, (b) ++a; *a = t;, (c) *r = t;?


Also see the follow-up: Dereference-assignment to a doubly incremented OutputIterator

1 Answers
Related