Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

--begin(container) - is defined behavier?

Tags:

c++

iterator

stl

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

like image 554
Leonid Volnitsky Avatar asked Dec 20 '22 15:12

Leonid Volnitsky


2 Answers

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.

like image 148
Benjamin Lindley Avatar answered Dec 24 '22 00:12

Benjamin Lindley


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.

like image 37
Dietmar Kühl Avatar answered Dec 24 '22 00:12

Dietmar Kühl