[libcxx-commits] [libcxx] Add C++23 stacktrace (P0881R7) (PR #136528)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 1 03:19:06 PDT 2025


================
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++23
+
+/*
+  (19.6.4.3) Observers [stacktrace.basic.obs]
+
+  const_reference at(size_type) const;
+*/
+
+#include <cassert>
+#include <stacktrace>
+
+_LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE std::stacktrace test1() { return std::stacktrace::current(0, 4); }
+_LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE std::stacktrace test2() { return test1(); }
+_LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE std::stacktrace test3() { return test2(); }
+
+_LIBCPP_NO_TAIL_CALLS
+int main(int, char**) {
+  auto st = test3();
+
+  assert(st.at(0));
+  assert(st.at(1));
+  assert(st.at(2));
+  assert(st.at(3));
+
+  auto f0 = st.at(0);
----------------
Zingam wrote:

AFAIK In libc++ we prefer to write the exact type specifier and to avoid `auto` if possible. Also here (and everywhere applicable) you should check the return type of the function, e.g. in C++20 and above:
```suggestion
  std::same_as<std::stacktrace::const_reference> decltype(auto) f0 = st.at(0);
```

https://github.com/llvm/llvm-project/pull/136528


More information about the libcxx-commits mailing list