std::basic_stacktrace(3)
πΈ NAME πΈ
Bestie, this is the whole std::basic_stacktrace(3) situation from the C++ Standard Libary (their spelling, not mine π) β and the NAME entry is literally std::basic_stacktrace β std::basic_stacktrace. Yes, icon behavior. We're about to get into it. π
πΈ Synopsis πΈ
Okay so, Defined in header <stacktrace> β here's the declaration vibe:
Defined in header <stacktrace>
template< class Allocator >
class basic_stacktrace;
(1) (since C++23)
using stacktrace =
std::basic_stacktrace<std::allocator<std::stacktrace_entry>>;
(2) (since C++23)
namespace pmr {
using stacktrace =
std::basic_stacktrace<std::pmr::polymorphic_allocator<std::stacktrace_entry>>;
}
(3) (since C++23)
- The
basic_stacktraceclass template represents a snapshot of the whole stacktrace or its given part. It satisfies the requirement ofAllocatorAwareContainer,SequenceContainer, andReversibleContainer, 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. - Convenience type alias for the
basic_stacktraceusing the defaultstd::allocator. - Convenience type alias for the
basic_stacktraceusing the polymorphic allocator.
The invocation sequence of the current evaluation x[0] (the original math writes {x}_{0}) in the current thread of execution is a sequence ({x}_{0}, ..., {x}_{n}) aka (x[0], ..., x[n]) of evaluations such that, for i β₯ 0, {x}_{i} aka x[i] is within the function invocation {x}_{i+1} aka x[i+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 ofAllocator. The program is ill-formed ifAllocator::value_typeis notstd::stacktrace_entry. π«Ά
π Member types π
value_typeβstd::stacktrace_entryconst_referenceβconst value_type&referenceβvalue_type&const_iteratorβ implementation-defined constLegacyRandomAccessIteratortype that modelsrandom_access_iteratoriteratorβconst_iteratorreverse_iteratorβstd::reverse_iterator<iterator>reverse_const_iteratorβstd::reverse_iterator<const_iterator>difference_typeβ implementation-defined signed integer typesize_typeβ implementation-defined unsigned integer typeallocator_typeβAllocator
π οΈ Member functions π οΈ
- constructor β creates a new
basic_stacktrace(public member function) - destructor β destroys the
basic_stacktrace(public member function) operator=β assigns to thebasic_stacktrace(public member function)currentβ obtains the current stacktrace or its given part (public static member function)get_allocatorβ returns the associated allocator (public member function)
π¦ Iterators π¦
beginβ returns an iterator to the beginning (public member function)cbeginβ public member functionendβ returns an iterator to the end (public member function)cendβ public member functionrbeginβ returns a reverse iterator to the beginning (public member function)crbeginβ public member functionrendβ returns a reverse iterator to the end (public member function)crendβ public member function
π¦ Capacity π¦
emptyβ checks whether thebasic_stacktraceis empty (public member function)sizeβ returns the number of stacktrace entries (public member function)max_sizeβ returns the maximum possible number of stacktrace entries (public member function)
πͺ Element access πͺ
operator[]β access specified stacktrace entry (public member function)atβ access specified stacktrace entry with bounds checking (public member function)
π Modifiers π
swapβ swaps the contents (public member function)
π«Ά Non-member functions π«Ά
operator==β compares the sizes and the contents of twobasic_stacktracevalues (function template)operator<=>(C++23)β compares the sizes and the contents of twobasic_stacktracevalues (function template)std::swap(std::basic_stacktrace)(C++23)β specializes thestd::swapalgorithm (function template)to_string(C++23)β returns a string with a description of thebasic_stacktrace(function template)operator<<(C++23)β performs stream output ofbasic_stracktrace(function template)
yes bestie, basic_stracktrace is exactly how it's written there β we are preserving the moment, not fixing it ππ
π§ Helper classes π§
std::hash<std::basic_stacktrace>(C++23)β hash support forstd::basic_stacktrace(class template specialization)std::formatter<std::basic_stacktrace>(C++23)β formatting support forbasic_stacktrace(class template specialization)
π 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. No cap.
boost::stacktrace::basic_stacktrace (available in Boost.Stacktrace) can be used instead when std::basic_stacktrace is not available. Backup plan behavior π
π¬ Feature-test macros π¬
__cpp_lib_stacktraceβ value202011Lβ Std(C++23)β feature: Stacktrace library__cpp_lib_formattersβ value202302Lβ Std(C++23)β feature: Formattingstd::thread::idandstd::stacktrace
π§ͺ Example π§ͺ
The output obtained using Compiler Explorer: msvc and gcc. π
// Run this code
#include <iostream>
#include <stacktrace>
int nested_func(int c)
{
std::cout << std::stacktrace::current() << '\n';
return c + 1;
}
int func(int b)
{
return nested_func(b + 1);
}
int main()
{
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 π
stacktrace_entry(C++23)β representation of an evaluation in a stacktrace (class)
mwah π β signed with love by http://cppreference.com, dated 2024.06.10, bestie π«Άβ¨ (that's the source/version and date, not me copying the footer ππ)
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms