From cea7c2f7839eef81ecbd6dd10d103190ef68875c Mon Sep 17 00:00:00 2001 From: kwiberg Date: Thu, 7 Jan 2016 05:52:04 -0800 Subject: [PATCH] Replace manual casting to rvalue reference with calls to std::move Review URL: https://codereview.webrtc.org/1570473002 Cr-Commit-Position: refs/heads/master@{#11163} --- webrtc/base/buffer.h | 2 +- webrtc/base/optional.h | 7 +++--- webrtc/base/optional_unittest.cc | 24 +++++++++---------- webrtc/base/scoped_ptr.h | 4 ++-- .../system_wrappers/include/scoped_vector.h | 6 ++--- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/webrtc/base/buffer.h b/webrtc/base/buffer.h index 076fa08f26..bf2e9f3c93 100644 --- a/webrtc/base/buffer.h +++ b/webrtc/base/buffer.h @@ -172,7 +172,7 @@ class Buffer { // b.Pass() does the same thing as std::move(b). Buffer&& Pass() { assert(IsConsistent()); - return static_cast(*this); + return std::move(*this); } // Resets the buffer to zero size and capacity. Works even if the buffer has diff --git a/webrtc/base/optional.h b/webrtc/base/optional.h index 6e7535b446..b8071e6358 100644 --- a/webrtc/base/optional.h +++ b/webrtc/base/optional.h @@ -66,20 +66,19 @@ class Optional final { // Construct an Optional that contains a value. explicit Optional(const T& val) : value_(val), has_value_(true) {} - explicit Optional(T&& val) - : value_(static_cast(val)), has_value_(true) {} + explicit Optional(T&& val) : value_(std::move(val)), has_value_(true) {} // Copy and move constructors. // TODO(kwiberg): =default the move constructor when MSVC supports it. Optional(const Optional&) = default; Optional(Optional&& m) - : value_(static_cast(m.value_)), has_value_(m.has_value_) {} + : value_(std::move(m.value_)), has_value_(m.has_value_) {} // Assignment. // TODO(kwiberg): =default the move assignment op when MSVC supports it. Optional& operator=(const Optional&) = default; Optional& operator=(Optional&& m) { - value_ = static_cast(m.value_); + value_ = std::move(m.value_); has_value_ = m.has_value_; return *this; } diff --git a/webrtc/base/optional_unittest.cc b/webrtc/base/optional_unittest.cc index 5483314f49..eabf091e17 100644 --- a/webrtc/base/optional_unittest.cc +++ b/webrtc/base/optional_unittest.cc @@ -162,7 +162,7 @@ TEST(OptionalTest, TestConstructMoveEmpty) { { Optional x; EXPECT_FALSE(x); - auto y = static_cast&&>(x); + auto y = std::move(x); EXPECT_FALSE(y); } EXPECT_EQ(V("0:0. default constructor", "1:0. move constructor (from 0:0)", @@ -176,7 +176,7 @@ TEST(OptionalTest, TestConstructMoveFull) { Optional x(Logger(17)); EXPECT_TRUE(x); log->push_back("---"); - auto y = static_cast&&>(x); + auto y = std::move(x); EXPECT_TRUE(x); EXPECT_TRUE(y); log->push_back("---"); @@ -289,7 +289,7 @@ TEST(OptionalTest, TestMoveAssignToEmptyFromEmpty) { auto log = Logger::Setup(); { Optional x, y; - x = static_cast&&>(y); + x = std::move(y); } EXPECT_EQ( V("0:0. default constructor", "1:1. default constructor", @@ -303,7 +303,7 @@ TEST(OptionalTest, TestMoveAssignToFullFromEmpty) { Optional x(Logger(17)); Optional y; log->push_back("---"); - x = static_cast&&>(y); + x = std::move(y); log->push_back("---"); } EXPECT_EQ( @@ -320,7 +320,7 @@ TEST(OptionalTest, TestMoveAssignToEmptyFromFull) { Optional x; Optional y(Logger(17)); log->push_back("---"); - x = static_cast&&>(y); + x = std::move(y); log->push_back("---"); } EXPECT_EQ(V("0:0. default constructor", "1:17. explicit constructor", @@ -336,7 +336,7 @@ TEST(OptionalTest, TestMoveAssignToFullFromFull) { Optional x(Logger(17)); Optional y(Logger(42)); log->push_back("---"); - x = static_cast&&>(y); + x = std::move(y); log->push_back("---"); } EXPECT_EQ( @@ -354,7 +354,7 @@ TEST(OptionalTest, TestMoveAssignToEmptyFromT) { Optional x; Logger y(17); log->push_back("---"); - x = Optional(static_cast(y)); + x = Optional(std::move(y)); log->push_back("---"); } EXPECT_EQ(V("0:0. default constructor", "1:17. explicit constructor", "---", @@ -370,7 +370,7 @@ TEST(OptionalTest, TestMoveAssignToFullFromT) { Optional x(Logger(17)); Logger y(42); log->push_back("---"); - x = Optional(static_cast(y)); + x = Optional(std::move(y)); log->push_back("---"); } EXPECT_EQ( @@ -390,13 +390,13 @@ TEST(OptionalTest, TestDereference) { log->push_back("---"); x->Foo(); y->Foo(); - static_cast&&>(x)->Foo(); - static_cast&&>(y)->Foo(); + std::move(x)->Foo(); + std::move(y)->Foo(); log->push_back("---"); (*x).Foo(); (*y).Foo(); - (*static_cast&&>(x)).Foo(); - (*static_cast&&>(y)).Foo(); + (*std::move(x)).Foo(); + (*std::move(y)).Foo(); log->push_back("---"); } EXPECT_EQ(V("0:42. explicit constructor", diff --git a/webrtc/base/scoped_ptr.h b/webrtc/base/scoped_ptr.h index db615f3089..3f1a87a73c 100644 --- a/webrtc/base/scoped_ptr.h +++ b/webrtc/base/scoped_ptr.h @@ -374,7 +374,7 @@ class scoped_ptr { scoped_ptr& operator=(const scoped_ptr& other) = delete; // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).) - scoped_ptr&& Pass() { return static_cast(*this); } + scoped_ptr&& Pass() { return std::move(*this); } // Reset. Deletes the currently owned object, if any. // Then takes ownership of a new object, if given. @@ -507,7 +507,7 @@ class scoped_ptr { scoped_ptr& operator=(const scoped_ptr& other) = delete; // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).) - scoped_ptr&& Pass() { return static_cast(*this); } + scoped_ptr&& Pass() { return std::move(*this); } // Reset. Deletes the currently owned array, if any. // Then takes ownership of a new object, if given. diff --git a/webrtc/system_wrappers/include/scoped_vector.h b/webrtc/system_wrappers/include/scoped_vector.h index 7336d98a86..284f437259 100644 --- a/webrtc/system_wrappers/include/scoped_vector.h +++ b/webrtc/system_wrappers/include/scoped_vector.h @@ -43,9 +43,7 @@ class ScopedVector { ~ScopedVector() { clear(); } // Move construction and assignment. - ScopedVector(ScopedVector&& other) { - *this = static_cast(other); - } + ScopedVector(ScopedVector&& other) { *this = std::move(other); } ScopedVector& operator=(ScopedVector&& other) { std::swap(v_, other.v_); // The arguments are std::vectors, so std::swap // is the one that we want. @@ -58,7 +56,7 @@ class ScopedVector { ScopedVector& operator=(const ScopedVector& other) = delete; // Get an rvalue reference. (sv.Pass() does the same thing as std::move(sv).) - ScopedVector&& Pass() { return static_cast(*this); } + ScopedVector&& Pass() { return std::move(*this); } reference operator[](size_t index) { return v_[index]; } const_reference operator[](size_t index) const { return v_[index]; }