[llvm] [ADT] Consolidate CallbacksHolder (NFC) (PR #160234)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 23 07:55:44 PDT 2025


================
@@ -236,17 +232,17 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
   // type erased behaviors needed. Create a static instance of the struct type
   // here and each instance will contain a pointer to it.
   // Wrap in a struct to avoid https://gcc.gnu.org/PR71954
-  template <typename CallableT, typename CalledAs, typename Enable = void>
-  struct CallbacksHolder {
-    inline static NonTrivialCallbacks Callbacks = {
-        &CallImpl<CalledAs>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
-  };
-  // See if we can create a trivial callback. We need the callable to be
-  // trivially moved and trivially destroyed so that we don't have to store
-  // type erased callbacks for those operations.
-  template <typename CallableT, typename CalledAs>
-  struct CallbacksHolder<CallableT, CalledAs, EnableIfTrivial<CallableT>> {
-    inline static TrivialCallback Callbacks = {&CallImpl<CalledAs>};
+  template <typename CallableT, typename CalledAs> struct CallbacksHolder {
+    inline static auto Callbacks = []() constexpr {
+      // For trivial callables, we don't need to store move and destroy
+      // callbacks.
+      if constexpr (std::is_trivially_move_constructible_v<CallableT> &&
+                    std::is_trivially_destructible_v<CallableT>)
+        return TrivialCallback{&CallImpl<CalledAs>};
+      else
+        return NonTrivialCallbacks{&CallImpl<CalledAs>, &MoveImpl<CallableT>,
+                                   &DestroyImpl<CallableT>};
+    }();
----------------
kuhar wrote:

Neat, TIL

https://github.com/llvm/llvm-project/pull/160234


More information about the llvm-commits mailing list