std::stacktrace, std::basic_stacktrace, c++

c++ std::stacktrace

The c++ std::stacktrace stack trace object is an object in programming that keeps track of the called functions until a particular point in the program. It keeps a log of the stack frames currently present in the function call stack of the program.

std::stacktrace_entry (class)

c++ std::stacktrace_entry represents the record of a single stack frame in the function call stack.

std::stacktrace (class)

c++ std::stacktrace class is the actual class that keeps the records of the stack frames along with their sequence of execution.

std::stacktrace:

static method: std::stacktrace::current(...)

std::stacktrace::current(alloc);

std::stacktrace::current(skip, alloc);

std::stacktrace::current(skip, max_depth, alloc);

Method: std::stacktrace::begin(), std::stacktrace::end()

It can be used for range-based for-loop

Method: .operator[]

Access specified single frame by index

Methd: bool .empty()

Is stacktrace empty?

Methd: .size()

Get stacktrace frame count

Method: .max_size()

Get possible max frame count

std::to_string(the_stacktrace)

Convert std::stacktrace object to std::string

.operator<=>

.operator==

c++ example:

import std;

namespace my_space
{
	class my_class
	{
	public:
		virtual ~my_class()
		{
			{
				std::stacktrace stacktrace{std::stacktrace::current()};
				std::cout << "----------------------------------------\n";
				std::cout << "size: " << stacktrace.size() << std::endl;
				std::cout << "possible max size: " << stacktrace.max_size() << std::endl;
				std::cout << "empty: " << std::boolalpha << stacktrace.empty() << std::endl;
				std::cout << stacktrace << std::endl;
				std::cout << "At 3: " << stacktrace[3] << std::endl;
				std::cout << "range-based for loop:" << std::endl;
				for (const std::stacktrace_entry & x: stacktrace)
					std::cout << x << std::endl;
			}
			{
				std::stacktrace stacktrace{std::stacktrace::current(0, 1000)};
				std::cout << "Real max size: " << stacktrace.size() << std::endl;
			}
			{
				std::stacktrace stacktrace = std::stacktrace::current(2, 3);
				std::cout << "----------------------------------------\n";
				std::cout << "From 2 to 2+3:\n";
				std::cout << stacktrace << std::endl;
				std::cout << "std::to_string:" << std::endl << std::to_string(stacktrace) << std::endl;
			}
		}
	};
}

int main()
{
	my_space::my_class my_object;
}

Written: Tue May 13 02:20:45 AM UTC 2025

Up: c++