Leonid Volnitsky
Leonid Volnitsky

Reputation: 9144

--begin(container) - is defined behavier?

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()?

Upvotes: 4

Views: 112

Answers (2)

Dietmar Kühl
Dietmar Kühl

Reputation: 153830

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.

Upvotes: 3

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

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.

Upvotes: 8

Related Questions