Cem Kalyoncu
Cem Kalyoncu

Reputation: 14593

Should C++ Iterators be able to decrement after passing the last item?

I have been modifying my containers to be STL compatible. I have modified my iterators to have the necessary functions. They are all random access iterators. Currently they work fine with all applicable STL algorithms. But one of my iterators will not be functional once it became invalid (out of bounds). Is it necessary to have this property? Specifically I am afraid of end() iterator which cannot be decremented. Notice that it can be compared and even distance can be calculated with other valid iterators. Currently the size of the iterator is 4 bytes, I really dont want to add another 4 if it is not really necessary.

Thanks in advance,
Cem

Upvotes: 4

Views: 408

Answers (2)

AmokHuginnsson
AmokHuginnsson

Reputation: 1214

std::reverse_iterator depends on container.end() to be valid and being decrement-able.

Upvotes: 3

Björn Pollex
Björn Pollex

Reputation: 76778

For a random_access_iterator this is required. You will have to implement it. Specifically, according to 24.1.4.1 for a bidirectional_iterator (of which a random_access_iterator is a specialization) decrementing always has to be valid.

Upvotes: 4

Related Questions