I'm in need for some guidance regarding a question from a programming languages course I'm taking.
We need to come up with a way to implement an array class in C++, so that accessing it's element is statically checked for overflow. We are not to use C++11 (static assertion) nor any other black-box solution - this is a theoretical question, not something I need for coding purposes.
we did get a strong hint in the lecture slides :
" it is impossible to detect overflow of array indices when indices are of type integer – not if the type of the indices corresponds to the array size (which must be part of the type of the array). "
I thought about using fixed-length (array size) string for indices, but other than thinking about the option I really did not get much :(.
Clarification: OK, this has got some confused replies to it, probably because I was unclear - So I will re-emphasize:
- "Statically" means "at compile time". The compiler should alert the user of the overflow (Warning/Error..). Not runtime!
- Read the "hint" given to us - it's to be used. specifically, the program may NOT check for bounds! specifically the at() method of arrays in c++ is not to be used or emulated.
In light of all this what I am thinking they want is some sort of transformation int->(Indices type) that somehow fails or computes wrong values for these indices in case the array is overflowed.
Hope that is clearer now. Thank you's
std::array::at?