为什么 C++23 stacktrace_entry 不同于 source_location?

Why is C++23 stacktrace_entry different from source_location?

class stacktrace_entry {
public:
  string description() const;
  string source_file() const;
  uint_least32_t source_line() const;
  /* ... */
};
struct source_location {
  // source location field access
  constexpr uint_least32_t line() const noexcept;
  constexpr uint_least32_t column() const noexcept;
  constexpr const char* file_name() const noexcept;
  constexpr const char* function_name() const noexcept;
  /* ... */
};

它们的用途基本相同,为什么它们有差异,特别是 stacktrace_entry 中没有专栏,甚至不共享相同的 class?

They serve basically the same purpose,

它们的用途不同:如果有的话,整个 source_location 提供了特定的 compile-time 信息,而这些信息恰好是 stacktrace_entry 更广泛用例的一小部分(特别是 source_file 查询成员函数)。


P0881R7添加堆栈跟踪库的提案)介绍了更大范围的堆栈跟踪库或提供有关 run-time 期间调用序列的详细信息:

In the current working draft [N4741] there is no way to get, store and decode the current call sequence. Such call sequences are useful for debugging and post mortem debugging. [...]

This paper proposes classes that could simplify debugging and may change the assertion message into the following: [...]

Note on performance: during Boost.Stacktrace development phase many users requested a fast way to store stacktrace, without decoding the function names. This functionality is preserved in the paper. All the stack_frame functions and constructors are lazy and won't decode the pointer information if there was no explicit request from class user.

P1208R6 (Adopt source_location for C++20) 介绍 source_location 范围更窄,特别是提供某些信息源代码作为 compile-time 信息。

P0881 的审查提供了第 16 研究组关于库之间重叠的一些反馈:

SG16 discussed a number of options including the possibility of source_file() returning std::filesystem::path. SG16 converged on the following recommendation: "Align behavior with source_location; remove wording regarding conversion; string contents are implementation defined. ". No objection to unanimous consent.

然而,关注 stacktrace_entry class 的 source_file() 查询功能是为了与 source_location 的整体保持一致。再次强调 stacktrace_entrysource_location 的狭窄 compile-time 信息(如上所述,可以被视为 stacktrace_entry信息)。

提案作者的回答。

关于返回 std::string

As it is stated in P0881R7: "Unfortunately this is a necessarity on some platforms, where getting source line requires allocating or where source file name returned into a storage provided by user."

关于专栏:

Most popular solutions for non-Windows platforms do not provide a source_column(). At the moment std::stacktrace is a minimal subset of abilities of all platforms, so the column is missing. This could be changed later, the addition seems quite simple for C++ standardization.