From cppreference.com
Defined in header
template<classAllocator>classbasic_stacktrace; (1) (since C++23)usingstacktrace=std::basic_stacktrace<std::allocator<std::stacktrace_entry>>; (2) (since C++23)namespacepmr{usingstacktrace=std::basic_stacktrace<std::pmr::polymorphic_allocator<std::stacktrace_entry>>;} (3) (since C++23)1) The basic_stacktrace class template represents a snapshot of the whole stacktrace or its given part. It satisfies the requirement of
,
, and
, except that only move, assignment, swap, and operations for const-qualified sequence containers are supported, and the semantics of comparison functions are different from those required for a container.
2) Convenience type alias for the basic_stacktrace using the default
.
The invocation sequence of the current evaluation x0 in the current thread of execution is a sequence (x0, ..., xn) of evaluations such that, for i≥0, xi is within the function invocation xi+1.
A stacktrace is an approximate representation of an invocation sequence and consists of stacktrace entries.
A stacktrace entry represents an evaluation in a stacktrace. It is represented by std::stacktrace_entry in the C++ standard library.
Template parameters
Allocator - An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of
. The program is ill-formed if Allocator::value_type is not std::stacktrace_entry. Member types
Member type Definition value_typestd::stacktrace_entryconst_referenceconstvalue_type&referencevalue_type&const_iteratorimplementation-defined const
type that models
iteratorconst_iteratorreverse_iteratorstd::reverse_iterator<iterator>reverse_const_iteratorstd::reverse_iterator<const_iterator>difference_typeimplementation-defined signed integer type size_typeimplementation-defined unsigned integer type allocator_typeAllocatorMember functions
creates a new basic_stacktrace
(public member function)
destroys the basic_stacktrace
(public member function)
assigns to the basic_stacktrace
(public member function)
[static]
obtains the current stacktrace or its given part
(public static member function)
returns the associated allocator
(public member function)
Iterators
returns an iterator to the beginning
(public member function)
returns an iterator to the end
(public member function)
returns a reverse iterator to the beginning
(public member function)
returns a reverse iterator to the end
(public member function)
Capacity
checks whether the basic_stacktrace is empty
(public member function)
returns the number of stacktrace entries
(public member function)
returns the maximum possible number of stacktrace entries
(public member function)
Element access
access specified stacktrace entry
(public member function)
access specified stacktrace entry with bounds checking
(public member function)
Modifiers
swaps the contents
(public member function)
Non-member functions
Helper classes
Notes
Support for custom allocators is provided for using basic_stacktrace on a hot path or in embedded environments. Users can allocate stacktrace_entry objects on the stack or in some other place, where appropriate.
The sequence of std::stacktrace_entry objects owned by a std::basic_stacktrace is immutable, and either is empty or represents a contiguous interval of the whole stacktrace.
boost::stacktrace::basic_stacktrace (available in
) can be used instead when std::basic_stacktrace is not available.
macro ValueStdFeature
(C++23)
library
(C++23)Formatting
and std::stacktraceExample
The output obtained using Compiler Explorer:
and
.
Run this code
#include<iostream>#include<stacktrace>intnested_func(intc){std::cout<<std::stacktrace::current()<<'\n';returnc+1;}intfunc(intb){returnnested_func(b+1);}intmain(){std::cout<<func(777);}Possible output:
// msvc output (the lines ending with '⤶' arrows are split to fit the width): 0> C:\Users\ContainerAdministrator\AppData\Local\Temp\compiler-explorer-compiler20221122-⤶ 31624-2ja1sf.8ytzw\example.cpp(6): output_s!nested_func+0x1F 1> C:\Users\ContainerAdministrator\AppData\Local\Temp\compiler-explorer-compiler20221122-⤶ 31624-2ja1sf.8ytzw\example.cpp(12): output_s!func+0x15 2> C:\Users\ContainerAdministrator\AppData\Local\Temp\compiler-explorer-compiler20221122-⤶ 31624-2ja1sf.8ytzw\example.cpp(15): output_s!main+0xE 3> D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(288): output_s!⤶ __scrt_common_main_seh+0x10C 4> KERNEL32!BaseThreadInitThunk+0x14 5> ntdll!RtlUserThreadStart+0x21 779 gcc output: 0# nested_func(int) at /app/example.cpp:7 1# func(int) at /app/example.cpp:13 2# at /app/example.cpp:18 3# at :0 4# at :0 5# 779 See also