[llvm] fc01a55 - [Support] Refactor IsResizableBase with "constexpr if" (NFC) (#157326)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 7 10:39:57 PDT 2025


Author: Kazu Hirata
Date: 2025-09-07T10:39:53-07:00
New Revision: fc01a557557abdfa5031a1a3e30a17f606ed7970

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

LOG: [Support] Refactor IsResizableBase with "constexpr if" (NFC) (#157326)

We have two implementations of IsResizableBase that are selected with
a boolean template parameter.  This patch consolidates them into one
with "constexpr if".  The "constexpr if" condition uses
llvm::is_detected to check the availability of resize().

Added: 
    

Modified: 
    llvm/include/llvm/Support/YAMLTraits.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 00f645545a2be..27af2d60c837f 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -1850,39 +1850,29 @@ template <> struct IsFlowSequenceBase<true> {
   static const bool flow = true;
 };
 
-template <typename T, typename U = void>
-struct IsResizable : std::false_type {};
-
 template <typename T>
-struct IsResizable<T, std::void_t<decltype(std::declval<T>().resize(0))>>
-    : public std::true_type {};
-
-template <typename T, bool B> struct IsResizableBase {
-  using type = typename T::value_type;
-
-  static type &element(IO &io, T &seq, size_t index) {
-    if (index >= seq.size())
-      seq.resize(index + 1);
-    return seq[index];
-  }
-};
+using check_resize_t = decltype(std::declval<T>().resize(0));
 
-template <typename T> struct IsResizableBase<T, false> {
+template <typename T> struct IsResizableBase {
   using type = typename T::value_type;
 
   static type &element(IO &io, T &seq, size_t index) {
-    if (index >= seq.size()) {
-      io.setError(Twine("value sequence extends beyond static size (") +
-                  Twine(seq.size()) + ")");
-      return seq[0];
+    if constexpr (is_detected<check_resize_t, T>::value) {
+      if (index >= seq.size())
+        seq.resize(index + 1);
+    } else {
+      if (index >= seq.size()) {
+        io.setError(Twine("value sequence extends beyond static size (") +
+                    Twine(seq.size()) + ")");
+        return seq[0];
+      }
     }
     return seq[index];
   }
 };
 
 template <typename T, bool Flow>
-struct SequenceTraitsImpl : IsFlowSequenceBase<Flow>,
-                            IsResizableBase<T, IsResizable<T>::value> {
+struct SequenceTraitsImpl : IsFlowSequenceBase<Flow>, IsResizableBase<T> {
   static size_t size(IO &io, T &seq) { return seq.size(); }
 };
 


        


More information about the llvm-commits mailing list