最終更新日時(UTC):
が更新

履歴 編集

function
<stacktrace>

std::basic_stacktrace::current(C++23)

static basic_stacktrace
  current(const allocator_type& alloc = allocator_type()) noexcept; // (1) C++23

static basic_stacktrace
  current(size_type skip,
          const allocator_type& alloc = allocator_type()) noexcept; // (2) C++23

static basic_stacktrace
  current(size_type skip,
          size_type max_depth,
          const allocator_type& alloc = allocator_type()) noexcept; // (3) C++23

概要

現在位置からのスタックトレースを取得する。

  • (1) : 現在位置から現在の実行スレッドの初期関数までの全体のスタックトレースを取得する
  • (2) : 現在位置 + skipから実行スレッドの初期関数までのスタックトレースを取得する
  • (3) : 現在位置 + skipから最大でmax_depth個のスタックトレースを取得する

事前条件

  • (3) : skip <= skip + max_depthであること

戻り値

  • (1) :
    • 現在の実行スレッドでの、現在の評価のスタックトレースを保持するbasic_stacktraceオブジェクトを構築する
    • allocstacktrace_entryオブジェクトの配列を保持するstd::vector型メンバ変数に渡される
  • (2) :
    • basic_stacktrace::current(alloc)で構築されたオブジェクトstst.size()nとして、
    • イテレータ範囲[st.begin() + min(n, skip), st.end())allocを、stacktrace_entryオブジェクトの配列を保持するstd::vector型メンバ変数として保持する。ただし、その初期化に失敗した場合、basic_stacktraceオブジェクトは空になる
  • (3) :
    • basic_stacktrace::current(alloc)で構築されたオブジェクトstst.size()nとして、
    • イテレータ範囲[st.begin() + min(n, skip), st.begin() + min(n, skip + max_depth))allocを、stacktrace_entryオブジェクトの配列を保持するstd::vector型メンバ変数として保持する。ただし、その初期化に失敗した場合、basic_stacktraceオブジェクトは空になる

備考

  • (1) : 正常にスタックトレースを取得できた場合、構築されたbasic_stacktrace型のオブジェクトstの先頭要素st[0]は、現在の評価を表すstacktrace_entryオブジェクトであり、末尾要素st[st.size() - 1]は現在の実行スレッドの初期関数を表すstacktrace_entryオブジェクトである

全体のスタックトレースを取得する

#include <iostream>
#include <stacktrace>

void g() {
  std::cout << std::stacktrace::current() << std::endl;
}

void f() {
  g();
}

int main() {
  f();
}

出力例

 0# g() at main.cpp:5
 1# f() at main.cpp:9
 2# main at main.cpp:13

現在位置からN個を除いたスタックトレースを取得する

#include <iostream>
#include <stacktrace>

void g() {
  std::cout << std::stacktrace::current(1) << std::endl;
}

void f() {
  g();
}

int main() {
  f();
}

出力例

 0# f() at main.cpp:9
 1# main at main.cpp:13

指定範囲のスタックトレースを取得する

#include <iostream>
#include <stacktrace>

void g() {
  std::cout << std::stacktrace::current(1, 2) << std::endl;
}

void f() {
  g();
}

int main() {
  f();
}

出力例

 0# f() at main.cpp:9
 1# main at main.cpp:13

バージョン

言語

  • C++23

処理系