std::operator<<(std::basic_stacktrace)

来自cppreference.com
 
 
 
 
在标头 <stacktrace> 定义
template< class CharT, class Traits, class Allocator >

std::basic_ostream<CharT, Traits>& operator<<( std::basic_ostream<CharT, Traits>& os,

                                               const std::basic_stacktrace<Allocator>& st );
(C++23 起)

插入 st 的描述到输出流 os 中。等价于 return os << std::to_string(st);

参数

os - 输出流
st - 要插入其描述的 basic_stacktrace

返回值

os

异常

可能会抛出由实现定义的异常。

注解

由于 std::string 只能由 std::ostream 输出(例如不能由 std::wostream ),这等效地要求 os 的类型为 std::ostream&

示例

#include <stacktrace>
#include <iostream>
 
int main()
{
    std::cout << "The stacktrace obtained in the main function:\n";
    std::cout << std::stacktrace::current() << '\n';
    []{
        std::cout << "The stacktrace obtained in a nested lambda:\n";
        std::cout << std::stacktrace::current() << '\n';
    }();
}

可能的输出:

The stacktrace obtained in the main function:
 0# 0x0000000000402E7B in ./prog.exe
 1# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
 2# 0x0000000000402CD9 in ./prog.exe
 
The stacktrace obtained in a nested lambda:
 0# 0x0000000000402DDA in ./prog.exe
 1# 0x0000000000402EB2 in ./prog.exe
 2# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
 3# 0x0000000000402CD9 in ./prog.exe

参阅

进行 stacktrace_entry 的流输出
(函数模板)