From 0fd618d490aa1bf59d1b75e81b7615561e20c46c Mon Sep 17 00:00:00 2001 From: Hannes Roth Date: Thu, 19 Feb 2015 13:10:26 -0800 Subject: [PATCH] (Wangle) Clean up move constructors Summary: These were doing more assignments than necessary. 1) We don't need to set `core_` to `nullptr` and then immediately overwrite it with `other.core_`. 2) Don't need to swap the booleans (this was actually an unitialized memory access). Test Plan: Ran all the tests. Reviewed By: hans@fb.com Subscribers: trunkagent, folly-diffs@, jsedgwick, yfeldblum FB internal diff: D1852386 Signature: t1:1852386:1424375154:25997209e76ccd16169125597e868932a5143ffb --- folly/futures/Future-inl.h | 9 +++++---- folly/futures/Future.h | 2 +- folly/futures/Promise-inl.h | 12 +++++++----- folly/futures/Promise.h | 4 ++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index a8454155..516eb502 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -32,13 +32,14 @@ namespace detail { } template -Future::Future(Future&& other) noexcept : core_(nullptr) { - *this = std::move(other); +Future::Future(Future&& other) noexcept : core_(other.core_) { + other.core_ = nullptr; } template -Future& Future::operator=(Future&& other) { - std::swap(core_, other.core_); +Future& Future::operator=(Future&& other) noexcept { + core_ = other.core_; + other.core_ = nullptr; return *this; } diff --git a/folly/futures/Future.h b/folly/futures/Future.h index 1ac7e652..c351416b 100644 --- a/folly/futures/Future.h +++ b/folly/futures/Future.h @@ -192,7 +192,7 @@ class Future { // movable Future(Future&&) noexcept; - Future& operator=(Future&&); + Future& operator=(Future&&) noexcept; // makeFuture template diff --git a/folly/futures/Promise-inl.h b/folly/futures/Promise-inl.h index b347486a..1f4b7aa3 100644 --- a/folly/futures/Promise-inl.h +++ b/folly/futures/Promise-inl.h @@ -29,14 +29,16 @@ Promise::Promise() : retrieved_(false), core_(new detail::Core()) {} template -Promise::Promise(Promise&& other) : core_(nullptr) { - *this = std::move(other); +Promise::Promise(Promise&& other) noexcept + : retrieved_(other.retrieved_), core_(other.core_) { + other.core_ = nullptr; } template -Promise& Promise::operator=(Promise&& other) { - std::swap(core_, other.core_); - std::swap(retrieved_, other.retrieved_); +Promise& Promise::operator=(Promise&& other) noexcept { + retrieved_ = other.retrieved_; + core_ = other.core_; + other.core_ = nullptr; return *this; } diff --git a/folly/futures/Promise.h b/folly/futures/Promise.h index 1412577f..a223db0b 100644 --- a/folly/futures/Promise.h +++ b/folly/futures/Promise.h @@ -36,8 +36,8 @@ public: Promise& operator=(Promise const&) = delete; // movable - Promise(Promise&&); - Promise& operator=(Promise&&); + Promise(Promise&&) noexcept; + Promise& operator=(Promise&&) noexcept; /** Return a Future tied to the shared core state. This can be called only once, thereafter Future already retrieved exception will be raised. */ -- 2.34.1