On Thu, Apr 16, 2009 at 8:43 AM, Michael Goffioul <michael.goffioul@gmail.com> wrote:
On Thu, Apr 16, 2009 at 2:04 PM, Jens Vollinga <jensv@nikhef.nl> wrote:
I'm no C++ standard expert, but when I look at http://www.sgi.com/tech/stl/RandomAccessIterator.html my understanding of the precondition for "i += n" is that you cannot go after end().
It seems that the standard (I only have the draft. Hopefully there is no difference to the final version in the areas under consideration) and the SGI documentation deviate from each other. In SGI there are pre- and postconditions on +, -, +=, and -=. And these conditions are violated in the examples as you point out. But in the standard +, -, +=, -= have NO pre-/postconditions to them! See 24.1.5. The confusing part is, that -- and ++ DO have such conditions. But for the ++ operation, it is explicitly allowed to go past-the-end. See 24.1.4. So no problem for the code in factor.cpp. The code in utils.h seems to be against the standard, though, but I am no language lawyer and so I am maybe misinterpreting the conditions there. In case, this can be fixed by using reverse iterators.
OK, I had a look in a draft C++ standard document I could find on the web, and when reading 24.1.3, the "pre" condition for ++r is that r must be dereferenceable. When r == end(), it is not dereferenceable, so you cannot do ++r. Don't you have the same interpretation?
Yes. -- Gaby