While debugging something I stumbled into this behavior on godbolt. When compiling and running the following (godbolt link https://godbolt.org/z/9c5966sbK):
#include <array>
int main()
{
std::array<int, 4> arr{1,2,3,4};
int val = arr.at(5);
return val;
}
using latest x86 versions of gcc, clang and msvc, with optimizations disabled (-O0 and /Od resp.) msvc doesn't throw std::out_of_range exception.
Why does this happen?
main(),std::terminateis called. It's implementation- (and OS-) defined how that is handled, but it's not UB.