During active development of a class that uses std::vector
, it often happens that an index is out of bounds. (See this code review question for a practical example.) When using operator[]
, this results in undefined behavior. Still, the []
syntax is easy to read an more convenient than writing .at()
.
Therefore I'd like to write my code using the []
operator, but at the same time have bounds checks enabled. After testing the code, it should be very easy to remove the bounds checks.
I'm thinking of the following code:
util::bound_checked<std::vector<int>> numbers;
numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);
numbers.push_back(4);
std::cout << numbers[17] << "\n";
To me, this utility template seems to be so straight-forward that I'd expect it to exist. Does it? If so, under which name?