延迟end迭代器是否抛出?

问题描述

我正在尝试捕获此异常,但无法做到这一点:

#include <unordered_map>
#include <iostream>
#include <exception>


int main() {
    std::unordered_map<int,std::string> x;
    auto it = x.end();
    try { *it; }
    catch (const std::exception& exception) {
        std::cout << "BIG FAT ERROR" << std::endl;
    }
}

解决方法

未指定引发异常。它就是Undefined Behaviour。含义;如果这样做,则编译器对其生成的代码没有义务,并且整个程序无效,并且编译器也没有义务告诉您。