Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_stacktrace

      From cppreference.com
      <cpp‎ |utility
       
       
      Diagnostics library
       
       
      Defined in header<stacktrace>
      template<class Allocator>
      class basic_stacktrace;
      (1)(since C++23)
      using stacktrace=
          std::basic_stacktrace<std::allocator<std::stacktrace_entry>>;
      (2)(since C++23)
      namespace pmr{

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

      }
      (3)(since C++23)
      1) Thebasic_stacktrace class template represents a snapshot of the whole stacktrace or its given part. It satisfies the requirement ofAllocatorAwareContainer,SequenceContainer, andReversibleContainer, except that only move, assignment, swap, and operations for const-qualified sequence containers are supported, and the semantics of comparison functions are different from those required for a container.
      2) Convenience type alias for thebasic_stacktrace using the defaultstd::allocator.
      3) Convenience type alias for thebasic_stacktrace using thepolymorphic allocator.

      Theinvocation sequence of the current evaluation\(\small{ {x}_{0} }\)x0 in the current thread of execution is a sequence\(\small{ ({x}_{0}, \dots, {x}_{n})}\)(x0, ..., xn) of evaluations such that, for\(\small{i \ge 0}\)i≥0,\(\small{ {x}_{i} }\)xi is within the function invocation\(\small{ {x}_{i+1} }\)xi+1.

      Astacktrace is an approximate representation of an invocation sequence and consists of stacktrace entries.

      Astacktrace entry represents an evaluation in a stacktrace. It is represented bystd::stacktrace_entry in the C++ standard library.

      Contents

      [edit]Template parameters

      Allocator - An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements ofAllocator. The program is ill-formed ifAllocator::value_type is notstd::stacktrace_entry.

      [edit]Member types

      Member type Definition
      value_typestd::stacktrace_entry
      const_referenceconst value_type&
      referencevalue_type&
      const_iterator implementation-defined constLegacyRandomAccessIterator type that modelsrandom_access_iterator
      iteratorconst_iterator
      reverse_iteratorstd::reverse_iterator<iterator>
      reverse_const_iteratorstd::reverse_iterator<const_iterator>
      difference_type implementation-defined signed integer type
      size_type implementation-defined unsigned integer type
      allocator_typeAllocator

      [edit]Member functions

      creates a newbasic_stacktrace
      (public member function)[edit]
      destroys thebasic_stacktrace
      (public member function)[edit]
      assigns to thebasic_stacktrace
      (public member function)[edit]
      [static]
      obtains the current stacktrace or its given part
      (public static member function)[edit]
      returns the associated allocator
      (public member function)[edit]
      Iterators
      returns an iterator to the beginning
      (public member function)[edit]
      returns an iterator to the end
      (public member function)[edit]
      returns a reverse iterator to the beginning
      (public member function)[edit]
      returns a reverse iterator to the end
      (public member function)[edit]
      Capacity
      checks whether thebasic_stacktrace is empty
      (public member function)[edit]
      returns the number of stacktrace entries
      (public member function)[edit]
      returns the maximum possible number of stacktrace entries
      (public member function)[edit]
      Element access
      access specified stacktrace entry
      (public member function)[edit]
      access specified stacktrace entry with bounds checking
      (public member function)[edit]
      Modifiers
      swaps the contents
      (public member function)[edit]

      [edit]Non-member functions

      compares the sizes and the contents of twobasic_stacktrace values
      (function template)
      specializes thestd::swap algorithm
      (function template)[edit]
      (C++23)
      returns a string with a description of thebasic_stacktrace
      (function template)[edit]
      (C++23)
      performs stream output ofbasic_stracktrace
      (function template)[edit]

      [edit]Helper classes

      hash support forstd::basic_stacktrace
      (class template specialization)[edit]
      formatting support forbasic_stacktrace
      (class template specialization)[edit]

      [edit]Notes

      Support for custom allocators is provided for usingbasic_stacktrace on a hot path or in embedded environments. Users can allocatestacktrace_entry objects on the stack or in some other place, where appropriate.

      The sequence ofstd::stacktrace_entry objects owned by astd::basic_stacktrace is immutable, and either is empty or represents a contiguous interval of the whole stacktrace.

      boost::stacktrace::basic_stacktrace (available inBoost.Stacktrace) can be used instead whenstd::basic_stacktrace is not available.

      Feature-test macroValueStdFeature
      __cpp_lib_stacktrace202011L(C++23)Stacktrace library
      __cpp_lib_formatters202302L(C++23)Formattingstd::thread::id andstd::stacktrace

      [edit]Example

      The output obtained using Compiler Explorer:msvc andgcc.

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

      Possible output:

      // 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+0x1F1> C:\Users\ContainerAdministrator\AppData\Local\Temp\compiler-explorer-compiler20221122-⤶31624-2ja1sf.8ytzw\example.cpp(12): output_s!func+0x152> C:\Users\ContainerAdministrator\AppData\Local\Temp\compiler-explorer-compiler20221122-⤶31624-2ja1sf.8ytzw\example.cpp(15): output_s!main+0xE3> D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(288): output_s!⤶__scrt_common_main_seh+0x10C4> KERNEL32!BaseThreadInitThunk+0x145> ntdll!RtlUserThreadStart+0x21779 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

      [edit]See also

      representation of an evaluation in a stacktrace
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/basic_stacktrace&oldid=168072"

      [8]ページ先頭

      ©2009-2025 Movatter.jp