名前空間
変種
操作

std::basic_stacktrace

From cppreference.com
< cpp‎ | utility
 
 
 
 
ヘッダー <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修飾されたシーケンスコンテナに対するムーブ、代入、swap、および各種操作のみがサポートされる点、そして比較関数のセマンティクスがコンテナに要求されるものとは異なる点が例外です。
2) デフォルトの std::allocator を使用する basic_stacktrace のための便利な型エイリアスです。
3) 多相アロケータ を使用する basic_stacktrace のための便利な型エイリアスです。

現在の実行スレッドにおける現在の評価 x0呼び出しシーケンス (invocation sequence) とは、 i≥0 について、 xi が関数呼び出し xi+1 内にあるような、評価のシーケンス (x0, ..., xn) のことです。

スタックトレースは呼び出しシーケンスの近似表現であり、スタックトレースエントリから構成されます。

スタックトレースエントリは、スタックトレース内の評価を表現します。C++標準ライブラリでは std::stacktrace_entry によって表現されます。

目次

[編集] テンプレートパラメータ

アロケータ - メモリの確保/解放、およびそのメモリ上での要素の構築/破棄に使用されるアロケータ。この型は Allocator の要件を満たさなければなりません。Allocator::value_typestd::stacktrace_entry でない場合、プログラムは不適格 (ill-formed) となります。

[編集] メンバ型

メンバ型 定義
value_type std::stacktrace_entry
const_reference const value_type&
reference value_type&
const_iterator 処理系定義の random_access_iterator をモデル化する const LegacyRandomAccessIterator
iterator const_iterator
reverse_iterator std::reverse_iterator<iterator>
reverse_const_iterator std::reverse_iterator<const_iterator>
difference_type 処理系定義の符号付き整数型
size_type 処理系定義の符号なし整数型
allocator_type アロケータ

[編集] メンバ関数

新しい basic_stacktrace を生成する
(public member function) [編集]
basic_stacktrace を破棄する
(public member function) [編集]
basic_stacktrace に代入する
(public member function) [編集]
[static]
現在のスタックトレースまたはその一部を取得する
(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) [編集]

[編集] 非メンバ関数

2つの basic_stacktrace のサイズと内容を比較する
(関数テンプレート)
std::swap アルゴリズムを特殊化する
(function template) [編集]
(C++23)
basic_stacktrace の説明を含む文字列を返す
(function template) [編集]
basic_stracktrace のストリーム出力を行う
(function template) [編集]

[編集] ヘルパークラス

std::basic_stacktrace のハッシュサポート
(class template specialization) [編集]
basic_stacktrace のフォーマットサポート
(class template specialization) [編集]

[編集] ノート

カスタムアロケータのサポートは、ホットパスや組み込み環境で basic_stacktrace を使用するために提供されています。ユーザーは stacktrace_entry オブジェクトをスタック上や、その他適切な場所に確保することができます。

std::basic_stacktrace が所有する std::stacktrace_entry オブジェクトのシーケンスは不変 (immutable) であり、空であるか、スタックトレース全体の連続した区間を表現します。

std::basic_stacktrace が利用できない場合、代わりに boost::stacktrace::basic_stacktraceBoost.Stacktraceで利用可能)を使用できます。

機能テストマクロ 規格 機能
__cpp_lib_stacktrace 202011L (C++23) スタックトレース ライブラリ
__cpp_lib_formatters 202302L (C++23) std::thread::idstd::stacktrace のフォーマット

[編集]

Compiler Explorer を使用して得られた出力: msvc および gcc

#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

[編集] 関連項目

スタックトレース内の評価の表現
(クラス) [編集]
English 日本語 中文(简体) 中文(繁體)