Reputation: 13125
I have an input iterator which makes use of two methods getFirst and getNext. Both of these function are part of an api and theoretically these functions can fail in their operation for whatever reason. In this case I'm casting a runtime exception.
I've never used Exception Specifications in C++ previously but was thinking now might be a good time to start. So my Constructor and operator++ functions could specify that they can throw a runtime error.
Then I did a quick search in my vector.h file (std::vector) but I dont see throw used there. How come?
If I have reached the end of my iterator and I do *(i++) should there be an exception?
Upvotes: 0
Views: 96
Reputation: 1252
If I have reached the end of my iterator and I do *(i++) should there be an exception?
Yes thats an error.
About exception specification its better to specify when your method does not throw. and not the opposite. Take a look at this question.
Upvotes: 2
Reputation: 15175
The iterator actually cannot know when it has reached the end of the container because the STL iterators have no knowledge about the containers they point to.
So trying to increment it and dereference it "might" throw due to accessing invalid memory but there is no way of knowing if the next index is valid or not.
Upvotes: 0