std::stacktrace_entry, c++

std::stacktrace_entry (class)

The std::stacktrace_entry class provides operations for querying information about an evaluation in a stacktrace. Each stacktrace_entry object is either empty, or represents an evaluation in a stacktrace.

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

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

Method: .description()

Get frame description

Method: .source_file()

Get related source file name

Method: .source_line()

Get related source file line number

Method: .operator bool

Check if the frame is empty

std::to_string(entry)

Convert to std::string

.operator<=>

.operator==

c++ example:

import std;

namespace my_space
{
	class my_base_class
	{
	public:
		void print(const std::stacktrace & stacktrace) const
		{
			for (const std::stacktrace_entry & frame: stacktrace)
			{
				std::cout << frame << "\n=>" << std::endl;
				if (! frame)
					std::cout << "<This frame is empty>" << std::endl;
				std::cout
					<< "\tdescription: " << frame.description() << '\n'
					<< "\tsource file: " << frame.source_file() << '\n'
					<< "\tsource line: " << frame.source_line() << '\n'
				;
			}
		}
	public:
		virtual ~my_base_class()
		{
		}
	};

	class my_derived_class: virtual public my_base_class
	{
	public:
		~my_derived_class()
		{
			std::stacktrace stacktrace = std::stacktrace::current();
			std::cout << "----------------------------------------" << std::endl;
			std::cout << stacktrace << std::endl;
			std::cout << "----------------------------------------" << std::endl;
			this->print(stacktrace);
		}
	};
}

int main()
{
	auto ptr = new my_space::my_derived_class;
	delete ptr;
	ptr = nullptr;
	std::cout << "---- returned ----\n";
}

Written: Tue May 13 02:34:38 AM UTC 2025

Up: c++