"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
Andre Kostur <nn******@kostur.net> wrote in
news:Xn*******************************@207.35.177. 134:
di*****@gmail.com (Tony Di Croce) wrote in
news:37**************************@posting.google.c om:
Given:
T& operator [] ( int index ); // T is a parameterized type
What should be returned when index is out of range?
An exception. Or leave it as undefined behaviour.
This same question comes up virtually everywhere I return a
reference. I need to return a reference (as opposed to a copy)
because I want to use this operator as an l-value sometimes...
Does anyone know what does the STL does?
Undefined behaviour. One of the strong points of references is that
they _cannot_ be "NULL" (at least not without invoking undefined
behaviour).
Oops.. I'm thinking specifically of vectors. For map and set that would
create a default-constructed object at that index and return a reference
to that....
You may as well increase your vector size up to the specified index.
That way you would get about the same behavior you just described for map
and set.
Anyway, if the vector is not able to allocate, it will result in an
axception of some sort.
So better be prepared for an exception anyway.