名稱空間
變體
操作

std::basic_stacktrace

來自 cppreference.com
< cpp‎ | 工具
 
 
 
 
定義於標頭檔案 <stacktrace>
template< class Allocator >
class basic_stacktrace;
(1) (C++23 起)
using stacktrace =
    std::basic_stacktrace<std::allocator<std::stacktrace_entry>>;
(2) (C++23 起)
namespace pmr {

using stacktrace =
    std::basic_stacktrace<std::pmr::polymorphic_allocator<std::stacktrace_entry>>;

}
(3) (C++23 起)
1) basic_stacktrace 類模板表示整個堆疊跟蹤或其給定部分的快照。它滿足 AllocatorAwareContainerSequenceContainerReversibleContainer 的要求,但只支援移動、賦值、交換以及針對 const 限定序列容器的操作,並且比較函式的語義與容器所需的不同。
2) 使用預設 std::allocatorbasic_stacktrace 的便捷類型別名。
3) 使用 多型分配器basic_stacktrace 的便捷類型別名。

當前執行執行緒中當前求值 x0 的**呼叫序列**是一個求值序列 (x0, ..., xn),使得對於 i≥0xi 位於函式呼叫 xi+1 中。

**堆疊跟蹤**是呼叫序列的近似表示,由堆疊跟蹤條目組成。

**堆疊跟蹤條目**表示堆疊跟蹤中的一個求值。在 C++ 標準庫中,它由 std::stacktrace_entry 表示。

目錄

[編輯] 模板引數

Allocator - 用於獲取/釋放記憶體以及在該記憶體中構造/銷燬元素的分配器。該型別必須滿足 Allocator 的要求。如果 Allocator::value_type 不是 std::stacktrace_entry,則程式格式錯誤。

[編輯] 成員型別

成員型別 定義
value_type std::stacktrace_entry
const_reference const value_type&
reference value_type&
const_iterator 實現定義的 const LegacyRandomAccessIterator 型別,它建模 random_access_iterator
iterator const_iterator
reverse_iterator std::reverse_iterator<iterator>
reverse_const_iterator std::reverse_iterator<const_iterator>
difference_type 實現定義的有符號整數型別
size_type 實現定義的無符號整數型別
allocator_type Allocator

[編輯] 成員函式

建立一個新的 basic_stacktrace
(public member function) [編輯]
銷燬 basic_stacktrace
(public member function) [編輯]
賦值給 basic_stacktrace
(public member function) [編輯]
[靜態]
獲取當前堆疊跟蹤或其給定部分
(public static member function) [編輯]
返回關聯的分配器
(public member function) [編輯]
迭代器
返回指向起始的迭代器
(public member function) [編輯]
返回指向末尾的迭代器
(public member function) [編輯]
返回指向起始的逆向迭代器
(public member function) [編輯]
返回指向末尾的逆向迭代器
(public member function) [編輯]
容量
檢查 basic_stacktrace 是否為空
(public member function) [編輯]
返回堆疊跟蹤條目的數量
(public member function) [編輯]
返回堆疊跟蹤條目可能的最大數量
(public member function) [編輯]
元素訪問
訪問指定的堆疊跟蹤條目
(public member function) [編輯]
訪問指定的堆疊跟蹤條目,並進行邊界檢查
(public member function) [編輯]
修改器
交換內容
(public member function) [編輯]

[編輯] 非成員函式

比較兩個 basic_stacktrace 值的大小和內容
(函式模板)
特化 std::swap 演算法
(函式模板) [編輯]
(C++23)
返回描述 basic_stacktrace 的字串
(函式模板) [編輯]
執行 basic_stacktrace 的流輸出
(函式模板) [編輯]

[編輯] 輔助類

std::basic_stacktrace 的雜湊支援
(類模板特化) [編輯]
basic_stacktrace 的格式化支援
(類模板特化) [編輯]

[編輯] 注意

提供自定義分配器支援是為了在熱路徑或嵌入式環境中使用 basic_stacktrace。使用者可以在堆疊上或其他適當的位置分配 stacktrace_entry 物件。

std::basic_stacktrace 所擁有的 std::stacktrace_entry 物件序列是不可變的,並且要麼為空,要麼表示整個堆疊跟蹤的連續區間。

std::basic_stacktrace 不可用時,可以使用 boost::stacktrace::basic_stacktrace(在 Boost.Stacktrace 中可用)。

特性測試 標準 特性
__cpp_lib_stacktrace 202011L (C++23) 堆疊跟蹤
__cpp_lib_formatters 202302L (C++23) 格式化 std::thread::idstd::stacktrace

[編輯] 示例

使用 Compiler Explorer 獲得的輸出:msvcgcc

#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);
}

可能的輸出

// 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

[編輯] 另請參閱

棧追蹤中一次求值的表示
(類) [編輯]