[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:23:43 PDT 2026
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/222303
The implementation of `std::declval` calling `__declval` was originally introduced for improved compile times. However, we now have a builtin for `add_rvalue_reference`, which makes the old implementation faster again, since we avoid instantiating any class types _and_ avoid overload resolution.
>From 4c640f5306690389c20035f101f457ad3ddd8dff 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 | 14 +++-----------
1 file changed, 3 insertions(+), 11 deletions(-)
diff --git a/libcxx/include/__utility/declval.h b/libcxx/include/__utility/declval.h
index d0856b8afa4db..10256f1529af8 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
@@ -17,18 +18,9 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-// Suppress deprecation notice for volatile-qualified return type resulting
-// 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.");
}
More information about the libcxx-commits
mailing list