[llvm] [ADT] Add static_assert to llvm::to_address for function types (PR #166505)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 4 22:36:22 PST 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/166505
This patch aligns llvm::to_address with C++20 std::to_address by
adding a static_assert to prevent instantiation with function types.
The C++20 standard says that std::to_address is ill-formed on a
function type.
>From ff692f12b3ae6d98d010662554c04a1832c60dc1 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 4 Nov 2025 12:39:21 -0800
Subject: [PATCH] [ADT] Add static_assert to llvm::to_address for function
types
This patch aligns llvm::to_address with C++20 std::to_address by
adding a static_assert to prevent instantiation with function types.
The C++20 standard says that std::to_address is ill-formed on a
function type.
---
llvm/include/llvm/ADT/STLForwardCompat.h | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h
index ad94cdede9288..b975a403cd042 100644
--- a/llvm/include/llvm/ADT/STLForwardCompat.h
+++ b/llvm/include/llvm/ADT/STLForwardCompat.h
@@ -142,7 +142,10 @@ struct identity // NOLINT(readability-identifier-naming)
/// The std::pointer_traits<>::to_address(p) variations of these overloads has
/// not been implemented.
template <class Ptr> auto to_address(const Ptr &P) { return P.operator->(); }
-template <class T> constexpr T *to_address(T *P) { return P; }
+template <class T> constexpr T *to_address(T *P) {
+ static_assert(!std::is_function_v<T>);
+ return P;
+}
//===----------------------------------------------------------------------===//
// Features from C++23
More information about the llvm-commits
mailing list