【问题标题】:Does deferencing an end() iterator throw? [duplicate]推迟 end() 迭代器会抛出吗? [复制]
【发布时间】:2020-08-22 19:08:49
【问题描述】:

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

#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;
    }
}

【问题讨论】:

  • 取消引用 end 迭代器展示 undefined behavior。任何事情都可以发生。
  • 好吧,它ispossible。但是代码生成效率很低,不是 C++ 特性。所以他们说“你不能这样做”。

标签: c++ exception throw


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 2017-07-05
    • 1970-01-01
    • 2017-10-18
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多