What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <stdexcept>
#include <vector>
int main() {
std::vector<int> v{5, 10, 15};
int sum = 0;
for (int i = 0; i <= 3; ++i) {
try {
sum += v.at(i);
} catch (const std::out_of_range&) {
std::cout << "bad index " << i << ", ";
}
}
std::cout << "sum " << sum << '\n';
return 0;
}
The loop runs to i = 3 – one past the end. at(3) throws instead of reading garbage, the handler reports it, and the valid elements still add up to 30.