From: Sven Over Date: Mon, 28 Mar 2016 21:13:24 +0000 (-0700) Subject: folly::fibers: do not move out of lvalue references X-Git-Tag: 2016.07.26~407 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=6f323462a05f5726838af752b15af84ee6b2211b;p=folly.git folly::fibers: do not move out of lvalue references Summary:If you pass an lvalue to folly::fibers::Fiber::setFunction or setFunctionFinally, you do not expect that those methods move the function out of your lvalue. This diff uses std::forward for perfect forwarding, so if an lvalue is passed, a copy is made. If and when when start using folly::Function for storing the functions in the Fiber object, passing an lvalue will trigger a compiler error because folly::Function is not copyable. That is a good thing, as it enforces calling setFunction with std::move if you use a named object, making clear that the named object gets moved out of. Often people will pass temporary objects (like a lambda defined i- place), which a rvalues anyway, so this will not be a problem anyway. Reviewed By: andriigrynenko Differential Revision: D3102685 fb-gh-sync-id: 87bf3135f7f6630766f97be351599ff488e4b796 fbshipit-source-id: 87bf3135f7f6630766f97be351599ff488e4b796 --- diff --git a/folly/experimental/fibers/Fiber-inl.h b/folly/experimental/fibers/Fiber-inl.h index 5366e686..68ee253e 100644 --- a/folly/experimental/fibers/Fiber-inl.h +++ b/folly/experimental/fibers/Fiber-inl.h @@ -22,7 +22,7 @@ namespace folly { namespace fibers { template void Fiber::setFunction(F&& func) { assert(state_ == INVALID); - func_ = std::move(func); + func_ = std::forward(func); state_ = NOT_STARTED; } @@ -30,8 +30,8 @@ template void Fiber::setFunctionFinally(F&& resultFunc, G&& finallyFunc) { assert(state_ == INVALID); - resultFunc_ = std::move(resultFunc); - finallyFunc_ = std::move(finallyFunc); + resultFunc_ = std::forward(resultFunc); + finallyFunc_ = std::forward(finallyFunc); state_ = NOT_STARTED; } diff --git a/folly/experimental/fibers/FiberManager-inl.h b/folly/experimental/fibers/FiberManager-inl.h index 1e6ed035..ff40265e 100644 --- a/folly/experimental/fibers/FiberManager-inl.h +++ b/folly/experimental/fibers/FiberManager-inl.h @@ -355,7 +355,7 @@ struct FiberManager::AddTaskFinallyHelper { public: Finally(G&& finally, FiberManager& fm) : - finally_(std::move(finally)), + finally_(std::forward(finally)), fm_(fm) { } @@ -430,12 +430,13 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) { auto finallyLoc = static_cast( static_cast(funcLoc + 1)); - new (finallyLoc) typename Helper::Finally(std::move(finally), *this); + new (finallyLoc) typename Helper::Finally(std::forward(finally), *this); new (funcLoc) typename Helper::Func(std::move(func), *finallyLoc); fiber->setFunctionFinally(std::ref(*funcLoc), std::ref(*finallyLoc)); } else { - auto finallyLoc = new typename Helper::Finally(std::move(finally), *this); + auto finallyLoc = + new typename Helper::Finally(std::forward(finally), *this); auto funcLoc = new typename Helper::Func(std::move(func), *finallyLoc); fiber->setFunctionFinally(std::ref(*funcLoc), std::ref(*finallyLoc));