C++ Reference ยท Memory and Utilities
C++ std::stacktrace class
Reference for std::stacktrace (C++23): capture and print the call stack, stacktrace_entry fields, linking requirements, and a portable fallback.
std::stacktrace (header <stacktrace>, C++23) captures the chain of function calls that led to the current point of the program. Each frame is a std::stacktrace_entry, which can tell you a description, a source file and a line number when debug information is available. The usual use is to print a trace when an error occurs, for example inside a logger or an exception constructor.
Support is uneven. GCC 14 and later ship the feature but need an extra library at link time (-lstdc++exp), MSVC supports it, and the quality of the names depends on debug information. The online runner in this site does not link that library, so a program that uses std::stacktrace fails with a link error here. The examples that call it are therefore shown read-only; the runnable examples below show the feature-test macro and a portable way to track calls by hand.
#include <stacktrace> std::cout << std::stacktrace::current() << '\n';
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class Allocator> class basic_stacktrace; using stacktrace = basic_stacktrace<std::allocator<stacktrace_entry>>; static stacktrace current(const allocator_type& = allocator_type()) noexcept; static stacktrace current(size_type skip, size_type max_depth, const allocator_type& = {}) noexcept;
A read-only example, which compiles with g++ -std=c++23 trace.cpp -lstdc++exp:
#include <iostream> #include <stacktrace> void inner() { std::cout << std::stacktrace::current() << '\n'; } void outer() { inner(); } int main() { outer(); }
| Name | Description |
|---|---|
current() |
Captures the current stack. The first frame is the function that called current(). |
skip, max_depth |
Overload that leaves out the first skip frames and keeps at most max_depth. |
size(), empty(), iteration |
A stacktrace is a read-only sequence of stacktrace_entry. |
std::to_string(trace), operator<< |
Text form, one numbered line per frame. |
entry.description(), source_file(), source_line() |
Per-frame details; empty strings and 0 when unknown. |
Returns: an empty trace if capturing is not possible (allocation failure or an unsupported platform) rather than an exception.
Errors: none thrown by current(); it is noexcept.
Usage notes
- Link requirement on GCC: add
-lstdc++exp(GCC 14 and 15) and compile with-gto get file and line numbers. Without debug info you only see addresses or function names. - Cost: capturing walks the stack and is slow, often microseconds per call, and printing is slower still because symbols must be resolved. Use it on error paths, not in hot loops.
- Inlining and optimization merge or remove frames, so optimized builds show shorter traces.
- Not a replacement for a debugger. It shows where you are, not argument values.
- Output format is implementation-defined and changes between compilers; do not parse it.
- Alternatives:
std::source_locationnames a single point, andbacktrace()or platform APIs offer traces on older toolchains. - Exceptions: C++ does not attach a trace automatically. A custom exception base class can capture
std::stacktrace::current()in its constructor.