From 220bd96840caee31eec1212b6c9309c7554f632d Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Thu, 11 Sep 2025 22:32:29 -0700 Subject: [PATCH] [ADT] Add C++20-style llvm::identity Currently, our llvm:identity is not quite like std::identity from C++20. Ours is a template struct while std::identity is a non-template struct with templatized operator(). This difference means that we cannot mechanically replace llvm::identity with std::identity when we switch to C++20 in the future. This patch implements llvm::identity that actually behaves like std::identity. The only difference is that we must refer to it as llvm::identity<> with <> at the end. Once this patch lands, I'm planning to migrate users of llvm::identity to the new style. There aren't that many: - 4 instances of SparseSet<...> - 3 instances of SparseMultiSet<...> - about 50 instances of IndexedMap<...> --- llvm/include/llvm/ADT/identity.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/ADT/identity.h b/llvm/include/llvm/ADT/identity.h index 88d033fc01141..9d599c6c192d8 100644 --- a/llvm/include/llvm/ADT/identity.h +++ b/llvm/include/llvm/ADT/identity.h @@ -15,10 +15,12 @@ #ifndef LLVM_ADT_IDENTITY_H #define LLVM_ADT_IDENTITY_H +#include + namespace llvm { -// Similar to `std::identity` from C++20. -template struct identity { +// Our legacy llvm::identity, not quite the same as std::identity. +template struct identity { using is_transparent = void; using argument_type = Ty; @@ -26,6 +28,19 @@ template struct identity { const Ty &operator()(const Ty &self) const { return self; } }; +// Forward-ported from C++20. +// +// While we are migrating from the legacy version, we must refer to this +// template as identity<>. Once the legacy version is removed, we can +// make this a non-template struct and remove the <>. +template <> struct identity { + using is_transparent = void; + + template constexpr T &&operator()(T &&self) const { + return std::forward(self); + } +}; + } // namespace llvm #endif // LLVM_ADT_IDENTITY_H