C++23 - What are the benefits of the stacktrace_entry class?

Viewed 938

When this class in the header file is added to the language, what problems will we be able to handle more easily and which syntaxes are planned to replace ? Below I am sharing a code I got from the cppreference web site.

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;
      };
    }
1 Answers

When you attach to a C++ program with a debugger and halt execution, one thing that is relatively easy to do (with some compile-time instrumentation) is work out the call stack of the code at a given point.

In general, the implementation of C++ is that the call stack is a linked list (possibly stored in a non-trivial way), where when you return from a function you jump to the spot the caller injected when it called you.

Those addresses can be decoded by the debugger, then the instruction locations can be mapped back to C++ source locations, and a pretty-print of how you got to this line of code can be generated. Depending on the optimization settings this information can sometimes be inaccurate, missing some frames, or complete nonsense; but it is very useful.

Even without the compile-time instrumentation, the chain of instruction pointers can be saved, and then someone with the compile-time instrumentation can decode it pretty well.

There are libraries that let C++ programs do this internally, without an external debugger. These include boost stacktrace's library.

This is adding that capability to the std library.

A stack trace is a chain of frames, and the frames can be mapped to source file, function name and line number information by this new piece of the standard library.

A typical use case might be to catch situations where the program is behaving in invalid ways in the C++ source code and generate a log to report it happening before you attempt to recover, or just do an exit. Then programmers can look at this stack trace and get information about how to fix that bug.

Related