[llvm] ac0f95a - [ADT] Simplify addEnumValues with llvm::to_underlying (NFC) (#163037)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 12 08:50:28 PDT 2025


Author: Kazu Hirata
Date: 2025-10-12T08:50:25-07:00
New Revision: ac0f95aa88a07434856decdfd6649b8853b5cdbf

URL: https://github.com/llvm/llvm-project/commit/ac0f95aa88a07434856decdfd6649b8853b5cdbf
DIFF: https://github.com/llvm/llvm-project/commit/ac0f95aa88a07434856decdfd6649b8853b5cdbf.diff

LOG: [ADT] Simplify addEnumValues with llvm::to_underlying (NFC) (#163037)

llvm::to_underlying, forward ported from C++23, conveniently packages
static_cast and std::underlying_type_t like so:

  static_cast<std::underlying_type_t<EnumTy>>(E)

Added: 
    

Modified: 
    llvm/include/llvm/ADT/STLExtras.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 5b20d6bd38262..658f26249122a 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -161,12 +161,10 @@ using TypeAtIndex = std::tuple_element_t<I, std::tuple<Ts...>>;
 /// Helper which adds two underlying types of enumeration type.
 /// Implicit conversion to a common type is accepted.
 template <typename EnumTy1, typename EnumTy2,
-          typename UT1 = std::enable_if_t<std::is_enum<EnumTy1>::value,
-                                          std::underlying_type_t<EnumTy1>>,
-          typename UT2 = std::enable_if_t<std::is_enum<EnumTy2>::value,
-                                          std::underlying_type_t<EnumTy2>>>
+          typename = std::enable_if_t<std::is_enum_v<EnumTy1> &&
+                                      std::is_enum_v<EnumTy2>>>
 constexpr auto addEnumValues(EnumTy1 LHS, EnumTy2 RHS) {
-  return static_cast<UT1>(LHS) + static_cast<UT2>(RHS);
+  return llvm::to_underlying(LHS) + llvm::to_underlying(RHS);
 }
 
 //===----------------------------------------------------------------------===//


        


More information about the llvm-commits mailing list