[PATCH] D102631: Allow incomplete template types in unique_function arguments

Fehr Mathieu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 19 08:55:00 PDT 2021


math-fehr updated this revision to Diff 346479.
math-fehr added a comment.

Address comments.

- Rename AdjustedParamT to AdjustedParamTBase and define AdjustedParamT alias
- Specialize also for rvalues
- Add static assert to make intentions clear
- Add rvalue tests, and renamed class to better express meaning


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102631/new/

https://reviews.llvm.org/D102631

Files:
  llvm/include/llvm/ADT/FunctionExtras.h
  llvm/unittests/ADT/FunctionExtrasTest.cpp


Index: llvm/unittests/ADT/FunctionExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/FunctionExtrasTest.cpp
+++ llvm/unittests/ADT/FunctionExtrasTest.cpp
@@ -273,4 +273,22 @@
   EXPECT_EQ("string", returns([] { return "hello"; }));
 }
 
+// A forward declared type, and a templated type.
+class Incomplete;
+template <typename T> class Templated { T A; };
+
+// Check that we can define unique_function that have references to
+// incomplete types, even if those types are templated over an
+// incomplete type.
+TEST(UniqueFunctionTest, IncompleteTypes) {
+  unique_function<void(Templated<Incomplete> &&)>
+      IncompleteArgumentRValueReference;
+  unique_function<void(Templated<Incomplete> &)>
+      IncompleteArgumentLValueReference;
+  unique_function<void(Templated<Incomplete> *)> IncompleteArgumentPointer;
+  unique_function<Templated<Incomplete> &()> IncompleteResultLValueReference;
+  unique_function<Templated<Incomplete> && ()> IncompleteResultRValueReference2;
+  unique_function<Templated<Incomplete> *()> IncompleteResultPointer;
+}
+
 } // anonymous namespace
Index: llvm/include/llvm/ADT/FunctionExtras.h
===================================================================
--- llvm/include/llvm/ADT/FunctionExtras.h
+++ llvm/include/llvm/ADT/FunctionExtras.h
@@ -89,13 +89,24 @@
   // The heuristic used is related to common ABI register passing conventions.
   // It doesn't have to be exact though, and in one way it is more strict
   // because we want to still be able to observe either moves *or* copies.
+  template <typename T> struct AdjustedParamTBase {
+    static_assert(!std::is_reference<T>::value,
+                  "references should be handled by template specialization");
+    using type = typename std::conditional<
+        llvm::is_trivially_copy_constructible<T>::value &&
+            llvm::is_trivially_move_constructible<T>::value &&
+            IsSizeLessThanThresholdT<T>::value,
+        T, T &>::type;
+  };
+
+  // This specialization ensures that 'AdjustedParam<V<T>&>' or
+  // 'AdjustedParam<V<T>&&>' does not trigger a compile-time error when 'T' is
+  // an incomplete type and V a templated type.
+  template <typename T> struct AdjustedParamTBase<T &> { using type = T &; };
+  template <typename T> struct AdjustedParamTBase<T &&> { using type = T &; };
+
   template <typename T>
-  using AdjustedParamT = typename std::conditional<
-      !std::is_reference<T>::value &&
-          llvm::is_trivially_copy_constructible<T>::value &&
-          llvm::is_trivially_move_constructible<T>::value &&
-          IsSizeLessThanThresholdT<T>::value,
-      T, T &>::type;
+  using AdjustedParamT = typename AdjustedParamTBase<T>::type;
 
   // The type of the erased function pointer we use as a callback to dispatch to
   // the stored callable when it is trivial to move and destroy.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102631.346479.patch
Type: text/x-patch
Size: 2898 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210519/32822377/attachment.bin>


More information about the llvm-commits mailing list