[libcxx-commits] [libcxx] [libc++] Simplify the implementation of std::declval (PR #222303)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 9 04:36:55 PDT 2026


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/222303

>From 873eb438aac2c8046911e7626d49cc7084efc552 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Wed, 9 Sep 2026 13:22:03 +0200
Subject: [PATCH] [libc++] Simplify the implementation of std::declval

---
 libcxx/include/__utility/declval.h | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/libcxx/include/__utility/declval.h b/libcxx/include/__utility/declval.h
index d0856b8afa4db..92069a41a8ef6 100644
--- a/libcxx/include/__utility/declval.h
+++ b/libcxx/include/__utility/declval.h
@@ -10,6 +10,7 @@
 #define _LIBCPP___UTILITY_DECLVAL_H
 
 #include <__config>
+#include <__type_traits/add_reference.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -21,17 +22,12 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 // from volatile-qualified types _Tp.
 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
 template <class _Tp>
-_Tp&& __declval(int);
-template <class _Tp>
-_Tp __declval(long);
-_LIBCPP_SUPPRESS_DEPRECATED_POP
-
-template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT {
-  static_assert(!__is_same(_Tp, _Tp),
+_LIBCPP_HIDE_FROM_ABI __add_rvalue_reference_t<_Tp> declval() _NOEXCEPT {
+  static_assert(false,
                 "std::declval can only be used in an unevaluated context. "
                 "It's likely that your current usage is trying to extract a value from the function.");
 }
+_LIBCPP_SUPPRESS_DEPRECATED_POP
 
 _LIBCPP_END_NAMESPACE_STD
 



More information about the libcxx-commits mailing list