C++23 - stacktrace_entry 类有什么好处?

标签 c++ c++23

当头文件中的这个类被添加到语言中时,我们将能够更轻松地处理哪些问题以及计划替换哪些语法?下面我将分享我从 cppreference 获得的代码网站。
类(class)std::stacktrace_entry

 namespace std {
      class stacktrace_entry {
      public:
        using native_handle_type = /* implementation-defined */;
     
        // constructors
        constexpr stacktrace_entry() noexcept;
        constexpr stacktrace_entry(const stacktrace_entry& other) noexcept;
        constexpr stacktrace_entry& operator=(const stacktrace_entry& other) noexcept;
     
        ~stacktrace_entry();
     
        // observers
        constexpr native_handle_type native_handle() const noexcept;
        constexpr explicit operator bool() const noexcept;
     
        // query
        string description() const;
        string source_file() const;
        uint_least32_t source_line() const;
     
        // comparison
        friend constexpr bool operator==(const stacktrace_entry& x,
                                         const stacktrace_entry& y) noexcept;
        friend constexpr strong_ordering operator<=>(const stacktrace_entry& x,
                                                     const stacktrace_entry& y) noexcept;
      };
    }

最佳答案

当您使用调试器附加到 C++ 程序并停止执行时,相对容易做的一件事(使用一些编译时检测)是在给定点计算代码的调用堆栈。
通常,C++ 的实现是调用堆栈是一个链表(可能以一种非平凡的方式存储),当您从函数返回时,您会跳转到调用者在调用您时注入(inject)的位置。
这些地址可以由调试器解码,然后指令位置可以映射回 C++ 源位置,并且可以生成您如何到达这行代码的 pretty-print 。根据优化设置,此信息有时可能不准确、丢失某些帧或完全无意义;但它非常有用。
即使没有编译时检测,也可以保存指令指针链,然后使用编译时检测的人可以很好地解码它。
有一些库可以让 C++ 程序在内部执行此操作,而无需外部调试器。其中包括 boost stacktrace 的库。
这是将该功能添加到 std图书馆。
堆栈跟踪是一连串的帧,这些帧可以通过标准库的这个新片段映射到源文件、函数名和行号信息。
一个典型的用例可能是捕捉程序在 C++ 源代码中以无效方式运行的情况,并在您尝试恢复之前生成日志以报告它发生的情况,或者只是退出。然后程序员可以查看此堆栈跟踪并获取有关如何修复该错误的信息。

关于C++23 - stacktrace_entry 类有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67368931/

相关文章:

c++ 添加两个 bool 数组

c++ - 使用 HDF5 C++ api 设置数据集的属性

c++ - 带结构的模板

C++ 模板 - 错误 : expected initializer before '<' token

c++ - C++23中省略参数列表的lambda表达式的有效性

c++ - C++中的split_view和lazy_split_view有什么区别?

c++ - 类型不完整的唯一指针 - C++2b?

c++ - 水平翻转图像 C++