Namespaces
Variants

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

From cppreference.net
定义于头文件 <stacktrace>
template < class Allocator >
std:: ostream & operator << ( std:: ostream & os, const std:: basic_stacktrace < Allocator > & st ) ;
(C++23 起)

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

目录

参数

os - 输出流
st - 待插入描述的 basic_stacktrace 对象

返回值

os

异常

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

示例

#include <stacktrace>
#include <iostream>
int main()
{
    std::cout << "在主函数中获取的堆栈跟踪:\n";
    std::cout << std::stacktrace::current() << '\n';
    []{
        std::cout << "在嵌套lambda中获取的堆栈跟踪:\n";
        std::cout << std::stacktrace::current() << '\n';
    }();
}

可能的输出:

在主函数中获取的堆栈跟踪:
 0# 0x0000000000402E7B in ./prog.exe
 1# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
 2# 0x0000000000402CD9 in ./prog.exe
在嵌套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

参阅

(C++23)
stacktrace_entry 执行流输出操作
(函数模板)