[libcxx-commits] [libcxx] [libc++] Add [[clang::lifetimebound]] to std::identity (PR #165854)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 31 04:10:59 PDT 2025


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/165854

None

>From 3119a8b49a0598b6f857fc55c3970cdda5e768bc Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Fri, 31 Oct 2025 12:10:39 +0100
Subject: [PATCH] [libc++] Add [[clang::lifetimebound]] to std::identity

---
 libcxx/include/__functional/identity.h         |  2 +-
 .../function.objects/lifetimebound.verify.cpp  | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 libcxx/test/libcxx/utilities/function.objects/lifetimebound.verify.cpp

diff --git a/libcxx/include/__functional/identity.h b/libcxx/include/__functional/identity.h
index 1b1c6cf73c378..02dde2b4f323d 100644
--- a/libcxx/include/__functional/identity.h
+++ b/libcxx/include/__functional/identity.h
@@ -44,7 +44,7 @@ struct __is_identity<reference_wrapper<const __identity> > : true_type {};
 
 struct identity {
   template <class _Tp>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator()(_Tp&& __t) const noexcept {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator()(_LIBCPP_LIFETIMEBOUND _Tp&& __t) const noexcept {
     return std::forward<_Tp>(__t);
   }
 
diff --git a/libcxx/test/libcxx/utilities/function.objects/lifetimebound.verify.cpp b/libcxx/test/libcxx/utilities/function.objects/lifetimebound.verify.cpp
new file mode 100644
index 0000000000000..0ca6007de530c
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/function.objects/lifetimebound.verify.cpp
@@ -0,0 +1,18 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// ADDITIONAL_COMPILE_FLAGS: -Wno-pessimizing-move -Wno-unused-variable
+
+#include <functional>
+
+#include "test_macros.h"
+
+void func() {
+  auto&& v1 = std::identity()(1); // expected-warning {{temporary bound to local reference 'v1' will be destroyed at the end of the full-expression}}
+}



More information about the libcxx-commits mailing list