use forward instead of move for universal references
authorSven Over <over@fb.com>
Sat, 23 Apr 2016 15:07:25 +0000 (08:07 -0700)
committerFacebook Github Bot 2 <facebook-github-bot-2-bot@fb.com>
Sat, 23 Apr 2016 15:20:24 +0000 (08:20 -0700)
Summary:When dealing with universal references, std::move will move from
objects that are passed as lvalue references. Instead std::forward
should be used, which only moves from an object if it's passed
as rvalue reference.

Reviewed By: yfeldblum

Differential Revision: D3200402

fb-gh-sync-id: 14be071e8498dd64cb8b2583c0cc2dd383bfebb8
fbshipit-source-id: 14be071e8498dd64cb8b2583c0cc2dd383bfebb8

folly/Conv.h
folly/Subprocess.h
folly/futures/Future.h
folly/test/ApplyTupleTest.cpp
folly/test/MPMCQueueTest.cpp

index ac2f5f2ba85e1b00cdbaa3b25b86b9233ae33b3d..79276ca4c4042f7c47ce655d8ffd40ee7f633e7b 100644 (file)
@@ -78,7 +78,7 @@ to(const Src & value) {
 template <class Tgt, class Src>
 typename std::enable_if<std::is_same<Tgt, Src>::value, Tgt>::type
 to(Src && value) {
-  return std::move(value);
+  return std::forward<Src>(value);
 }
 
 /*******************************************************************************
index e922ee0d261a8cfdeb348b95cbafb2d215eb1484..7598ee462665ff890c29722919ea75597c132b34 100644 (file)
@@ -662,7 +662,7 @@ class Subprocess {
       uint64_t maxLineLength = 0,  // No line length limit by default
       char delimiter = '\n',
       uint64_t bufSize = 1024
-    ) : fdLineCb_(std::move(fdLineCb)),
+    ) : fdLineCb_(std::forward<Callback>(fdLineCb)),
         maxLineLength_(maxLineLength),
         delimiter_(delimiter),
         bufSize_(bufSize) {}
index 49ff679a482e7410acc4da202d683867dc707f1c..990f2990b581904525ec82dcb1e535ece905d427 100644 (file)
@@ -331,7 +331,7 @@ class Future {
   template <class E>
   void raise(E&& exception) {
     raise(make_exception_wrapper<typename std::remove_reference<E>::type>(
-        std::move(exception)));
+        std::forward<E>(exception)));
   }
 
   /// Raise an interrupt. If the promise holder has an interrupt
index aad25c11d2a0ff7a3c17ed0a7754c6960e80f0fe..92ebac3fa1a8e919b293a4aded073d1b0f184bdd 100644 (file)
@@ -84,8 +84,8 @@ typedef GuardObjBase const& Guard;
 template<class F, class Tuple>
 struct GuardObj : GuardObjBase {
   explicit GuardObj(F&& f, Tuple&& args)
-    : f_(std::move(f))
-    , args_(std::move(args))
+    : f_(std::forward<F>(f))
+    , args_(std::forward<Tuple>(args))
   {}
   GuardObj(GuardObj&& g) noexcept
     : GuardObjBase(std::move(g))
index 7cd2452fbd7b4ad335b7de8cdb97d6e2a58ddd38..e3cd980c4f142f9d69ad3b1e85b8e9ac1541ecfd 100644 (file)
@@ -104,7 +104,7 @@ TEST(MPMCQueue, sequencer_deterministic) {
 template <typename T>
 void runElementTypeTest(T&& src) {
   MPMCQueue<T> cq(10);
-  cq.blockingWrite(std::move(src));
+  cq.blockingWrite(std::forward<T>(src));
   T dest;
   cq.blockingRead(dest);
   EXPECT_TRUE(cq.write(std::move(dest)));