If I try to access an std::vector
out of bounds it will lead to undefined behaviour.
const std::vector<int> vec {1,2,3};
const int a = vec[4];
Nevertheless, this code will probably not fail immediately, but a
will have an arbitrary value;
If vec
is accessed via the method .at()
an exception will be thrown.
const int a = vec.at(4);
The disadvantage is, that the performance will drop significantly if .at()
is always used.
My question is, if some C++ compiler supports a compile flag, that switches the operator []
to the .at()
method.
Turing it on would lead to good debug-able code. Turing it off would lead to a quick release executable.
I know that heap check tools , such as valgrind
will find these bugs, but they take a lot of time.