[PATCH] D83902: [ADT] Add a range-based version of std::move

Nathan James via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 13:57:34 PDT 2020


njames93 updated this revision to Diff 278897.
njames93 added a comment.

Removed std::string from test case in favour of custom moveable class.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D83902

Files:
  llvm/include/llvm/ADT/STLExtras.h
  llvm/unittests/ADT/STLExtrasTest.cpp


Index: llvm/unittests/ADT/STLExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/STLExtrasTest.cpp
+++ llvm/unittests/ADT/STLExtrasTest.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/Twine.h"
 #include "gtest/gtest.h"
 
 #include <list>
@@ -568,4 +569,41 @@
   EXPECT_FALSE(
       hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 10; }));
 }
+
+TEST(STLExtras, MoveRange) {
+  class Foo {
+    bool A;
+
+  public:
+    Foo() : A(true) {}
+    Foo(const Foo &) = delete;
+    Foo(Foo &&Other) : A(Other.A) { Other.A = false; }
+    Foo &operator=(const Foo &) = delete;
+    Foo &operator=(Foo &&Other) {
+      if (this != &Other) {
+        A = Other.A;
+        Other.A = false;
+      }
+      return *this;
+    }
+    operator bool() const { return A; }
+  };
+
+  constexpr size_t ItemCount = 4;
+  SmallVector<Foo, ItemCount> In, Out;
+  auto HasVal = [](const Foo &Item) { return static_cast<bool>(Item); };
+
+  In.resize(ItemCount);
+  EXPECT_TRUE(llvm::all_of(In, HasVal));
+
+  llvm::move(In, std::back_inserter(Out));
+
+  // Ensure input container is same size, but its contents were moved out.
+  EXPECT_EQ(In.size(), ItemCount);
+  EXPECT_TRUE(llvm::none_of(In, HasVal));
+
+  // Ensure output container has the contents of the input container.
+  EXPECT_EQ(Out.size(), ItemCount);
+  EXPECT_TRUE(llvm::all_of(Out, HasVal));
+}
 } // namespace
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1543,6 +1543,13 @@
   return std::copy(adl_begin(Range), adl_end(Range), Out);
 }
 
+/// Provide wrappers to std::move which take ranges instead of having to
+/// pass begin/end explicitly.
+template <typename R, typename OutputIt>
+OutputIt move(R &&Range, OutputIt Out) {
+  return std::move(adl_begin(Range), adl_end(Range), Out);
+}
+
 /// Wrapper function around std::find to detect if an element exists
 /// in a container.
 template <typename R, typename E>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83902.278897.patch
Type: text/x-patch
Size: 2195 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200717/180c3ba2/attachment.bin>


More information about the llvm-commits mailing list