4

I am implementing decrement operator for my custom bidirectional iterator. Is decrementing iterator pointing at 1st element of Range is defined behavior? Does it have some special value after decrementing, like Range.end()?

2

2 Answers 2

8

In the iterators of all of the standard library containers, that is undefined behavior. But if you are making your own iterator class, it doesn't have to be.

Sign up to request clarification or add additional context in comments.

2 Comments

The standard library almost has an example of this: std::forward_list has a before-begin iterator, but it's not reachable by decrementing the begin()
Ah, that is why you said almost, :). I did not know it is unreachable. Thanks for the info.
3

The iterator requirements are rather clear: In 24.2.6 [bidirectional.iterators], Table 110:

--r (Expression) X& (Return type) pre: there exists s such that r == ++s.

Since there is no such s for c.begin(), it can't be decremented without violating the precondition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.