I got an assignment to implement a template array with an iterator and to overlad some operators of my iterator and I have a few questions. From what i understand the std random accessed iterator allows u to exceed the array. Does it allow to get the value it is holding over the Size? Does it allow the user to write over that memory?
And for my implementation, should i allow access over the Size? should i allow to get the data out of the Size? should i allow any change to it?
what is considered good programming in this case?
any help will be much appreciated
I think you may be reading too much into your assignment, but your questions do bear consideration nonetheless.
First of all, some considerations:
iterator
and const_iterator
nested types in general), the former allowing writing to the pointed-to element, and the latter not.Now, with regard to your questions, there is indeed a design decision on whether to perform bounds checking or not.
In general, iterators in C++ are so lightweight that they perform no bounds checking at all. This is the name of efficiency and the you don't pay for what you don't use philosophy. If the user ever overshoot... she will fall prey to Undefined Behavior, that is anything could happen.
However, most (if not all) STL implementations will also acknowledge the issue and provide a debug mode (checked mode) that you can use when testing your application; of course those induce an additional cost, not only CPU-wise but also potentially memory-wise.
As such, it is your own choice whether to provide checks, or not, and possibly to provide it optionally. I would recommend starting off without checks, and only tack them on later on as you have a better idea of what the system looks like.