[llvm] [ADT] Use C++17 fold expression in PointerSumType (NFC) (PR #159560)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 18 04:55:24 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/159560

This patch simplifes the recursive Checker template with a C++17 fold
expression and "inlines" it right into the `static_assert`.


>From f7bd7401ca99852b585aebb50006a71cf752ee79 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 16 Sep 2025 11:13:20 -0700
Subject: [PATCH] [ADT] Use C++17 fold expression in PointerSumType (NFC)

This patch simplifes the recursive Checker template with a C++17 fold
expression and "inlines" it right into the `static_assert`.
---
 llvm/include/llvm/ADT/PointerSumType.h | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

diff --git a/llvm/include/llvm/ADT/PointerSumType.h b/llvm/include/llvm/ADT/PointerSumType.h
index 132eab58b112a..c4971bf3af87a 100644
--- a/llvm/include/llvm/ADT/PointerSumType.h
+++ b/llvm/include/llvm/ADT/PointerSumType.h
@@ -239,19 +239,9 @@ struct PointerSumTypeHelper : MemberTs... {
     TagMask = ~PointerMask
   };
 
-  // Finally we need a recursive template to do static checks of each
-  // member.
-  template <typename MemberT, typename... InnerMemberTs>
-  struct Checker : Checker<InnerMemberTs...> {
-    static_assert(MemberT::Tag < (1 << NumTagBits),
-                  "This discriminant value requires too many bits!");
-  };
-  template <typename MemberT> struct Checker<MemberT> : std::true_type {
-    static_assert(MemberT::Tag < (1 << NumTagBits),
-                  "This discriminant value requires too many bits!");
-  };
-  static_assert(Checker<MemberTs...>::value,
-                "Each member must pass the checker.");
+  // Finally, statically check each member.
+  static_assert(((MemberTs::Tag < (1 << NumTagBits)) && ...),
+                "A discriminant value requires too many bits!");
 };
 
 } // end namespace detail



More information about the llvm-commits mailing list