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}
This commit is contained in:
kwiberg
2016-01-07 05:52:04 -08:00
committed by Commit bot
parent a46a4c92d0
commit cea7c2f783
5 changed files with 20 additions and 23 deletions

View File

@ -172,7 +172,7 @@ class Buffer {
// b.Pass() does the same thing as std::move(b). // b.Pass() does the same thing as std::move(b).
Buffer&& Pass() { Buffer&& Pass() {
assert(IsConsistent()); assert(IsConsistent());
return static_cast<Buffer&&>(*this); return std::move(*this);
} }
// Resets the buffer to zero size and capacity. Works even if the buffer has // Resets the buffer to zero size and capacity. Works even if the buffer has

View File

@ -66,20 +66,19 @@ class Optional final {
// Construct an Optional that contains a value. // Construct an Optional that contains a value.
explicit Optional(const T& val) : value_(val), has_value_(true) {} explicit Optional(const T& val) : value_(val), has_value_(true) {}
explicit Optional(T&& val) explicit Optional(T&& val) : value_(std::move(val)), has_value_(true) {}
: value_(static_cast<T&&>(val)), has_value_(true) {}
// Copy and move constructors. // Copy and move constructors.
// TODO(kwiberg): =default the move constructor when MSVC supports it. // TODO(kwiberg): =default the move constructor when MSVC supports it.
Optional(const Optional&) = default; Optional(const Optional&) = default;
Optional(Optional&& m) Optional(Optional&& m)
: value_(static_cast<T&&>(m.value_)), has_value_(m.has_value_) {} : value_(std::move(m.value_)), has_value_(m.has_value_) {}
// Assignment. // Assignment.
// TODO(kwiberg): =default the move assignment op when MSVC supports it. // TODO(kwiberg): =default the move assignment op when MSVC supports it.
Optional& operator=(const Optional&) = default; Optional& operator=(const Optional&) = default;
Optional& operator=(Optional&& m) { Optional& operator=(Optional&& m) {
value_ = static_cast<T&&>(m.value_); value_ = std::move(m.value_);
has_value_ = m.has_value_; has_value_ = m.has_value_;
return *this; return *this;
} }

View File

@ -162,7 +162,7 @@ TEST(OptionalTest, TestConstructMoveEmpty) {
{ {
Optional<Logger> x; Optional<Logger> x;
EXPECT_FALSE(x); EXPECT_FALSE(x);
auto y = static_cast<Optional<Logger>&&>(x); auto y = std::move(x);
EXPECT_FALSE(y); EXPECT_FALSE(y);
} }
EXPECT_EQ(V("0:0. default constructor", "1:0. move constructor (from 0:0)", EXPECT_EQ(V("0:0. default constructor", "1:0. move constructor (from 0:0)",
@ -176,7 +176,7 @@ TEST(OptionalTest, TestConstructMoveFull) {
Optional<Logger> x(Logger(17)); Optional<Logger> x(Logger(17));
EXPECT_TRUE(x); EXPECT_TRUE(x);
log->push_back("---"); log->push_back("---");
auto y = static_cast<Optional<Logger>&&>(x); auto y = std::move(x);
EXPECT_TRUE(x); EXPECT_TRUE(x);
EXPECT_TRUE(y); EXPECT_TRUE(y);
log->push_back("---"); log->push_back("---");
@ -289,7 +289,7 @@ TEST(OptionalTest, TestMoveAssignToEmptyFromEmpty) {
auto log = Logger::Setup(); auto log = Logger::Setup();
{ {
Optional<Logger> x, y; Optional<Logger> x, y;
x = static_cast<Optional<Logger>&&>(y); x = std::move(y);
} }
EXPECT_EQ( EXPECT_EQ(
V("0:0. default constructor", "1:1. default constructor", V("0:0. default constructor", "1:1. default constructor",
@ -303,7 +303,7 @@ TEST(OptionalTest, TestMoveAssignToFullFromEmpty) {
Optional<Logger> x(Logger(17)); Optional<Logger> x(Logger(17));
Optional<Logger> y; Optional<Logger> y;
log->push_back("---"); log->push_back("---");
x = static_cast<Optional<Logger>&&>(y); x = std::move(y);
log->push_back("---"); log->push_back("---");
} }
EXPECT_EQ( EXPECT_EQ(
@ -320,7 +320,7 @@ TEST(OptionalTest, TestMoveAssignToEmptyFromFull) {
Optional<Logger> x; Optional<Logger> x;
Optional<Logger> y(Logger(17)); Optional<Logger> y(Logger(17));
log->push_back("---"); log->push_back("---");
x = static_cast<Optional<Logger>&&>(y); x = std::move(y);
log->push_back("---"); log->push_back("---");
} }
EXPECT_EQ(V("0:0. default constructor", "1:17. explicit constructor", EXPECT_EQ(V("0:0. default constructor", "1:17. explicit constructor",
@ -336,7 +336,7 @@ TEST(OptionalTest, TestMoveAssignToFullFromFull) {
Optional<Logger> x(Logger(17)); Optional<Logger> x(Logger(17));
Optional<Logger> y(Logger(42)); Optional<Logger> y(Logger(42));
log->push_back("---"); log->push_back("---");
x = static_cast<Optional<Logger>&&>(y); x = std::move(y);
log->push_back("---"); log->push_back("---");
} }
EXPECT_EQ( EXPECT_EQ(
@ -354,7 +354,7 @@ TEST(OptionalTest, TestMoveAssignToEmptyFromT) {
Optional<Logger> x; Optional<Logger> x;
Logger y(17); Logger y(17);
log->push_back("---"); log->push_back("---");
x = Optional<Logger>(static_cast<Logger&&>(y)); x = Optional<Logger>(std::move(y));
log->push_back("---"); log->push_back("---");
} }
EXPECT_EQ(V("0:0. default constructor", "1:17. explicit constructor", "---", EXPECT_EQ(V("0:0. default constructor", "1:17. explicit constructor", "---",
@ -370,7 +370,7 @@ TEST(OptionalTest, TestMoveAssignToFullFromT) {
Optional<Logger> x(Logger(17)); Optional<Logger> x(Logger(17));
Logger y(42); Logger y(42);
log->push_back("---"); log->push_back("---");
x = Optional<Logger>(static_cast<Logger&&>(y)); x = Optional<Logger>(std::move(y));
log->push_back("---"); log->push_back("---");
} }
EXPECT_EQ( EXPECT_EQ(
@ -390,13 +390,13 @@ TEST(OptionalTest, TestDereference) {
log->push_back("---"); log->push_back("---");
x->Foo(); x->Foo();
y->Foo(); y->Foo();
static_cast<Optional<Logger>&&>(x)->Foo(); std::move(x)->Foo();
static_cast<const Optional<Logger>&&>(y)->Foo(); std::move(y)->Foo();
log->push_back("---"); log->push_back("---");
(*x).Foo(); (*x).Foo();
(*y).Foo(); (*y).Foo();
(*static_cast<Optional<Logger>&&>(x)).Foo(); (*std::move(x)).Foo();
(*static_cast<const Optional<Logger>&&>(y)).Foo(); (*std::move(y)).Foo();
log->push_back("---"); log->push_back("---");
} }
EXPECT_EQ(V("0:42. explicit constructor", EXPECT_EQ(V("0:42. explicit constructor",

View File

@ -374,7 +374,7 @@ class scoped_ptr {
scoped_ptr& operator=(const scoped_ptr& other) = delete; scoped_ptr& operator=(const scoped_ptr& other) = delete;
// Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).) // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).)
scoped_ptr&& Pass() { return static_cast<scoped_ptr&&>(*this); } scoped_ptr&& Pass() { return std::move(*this); }
// Reset. Deletes the currently owned object, if any. // Reset. Deletes the currently owned object, if any.
// Then takes ownership of a new object, if given. // Then takes ownership of a new object, if given.
@ -507,7 +507,7 @@ class scoped_ptr<T[], D> {
scoped_ptr& operator=(const scoped_ptr& other) = delete; scoped_ptr& operator=(const scoped_ptr& other) = delete;
// Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).) // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).)
scoped_ptr&& Pass() { return static_cast<scoped_ptr&&>(*this); } scoped_ptr&& Pass() { return std::move(*this); }
// Reset. Deletes the currently owned array, if any. // Reset. Deletes the currently owned array, if any.
// Then takes ownership of a new object, if given. // Then takes ownership of a new object, if given.

View File

@ -43,9 +43,7 @@ class ScopedVector {
~ScopedVector() { clear(); } ~ScopedVector() { clear(); }
// Move construction and assignment. // Move construction and assignment.
ScopedVector(ScopedVector&& other) { ScopedVector(ScopedVector&& other) { *this = std::move(other); }
*this = static_cast<ScopedVector&&>(other);
}
ScopedVector& operator=(ScopedVector&& other) { ScopedVector& operator=(ScopedVector&& other) {
std::swap(v_, other.v_); // The arguments are std::vectors, so std::swap std::swap(v_, other.v_); // The arguments are std::vectors, so std::swap
// is the one that we want. // is the one that we want.
@ -58,7 +56,7 @@ class ScopedVector {
ScopedVector& operator=(const ScopedVector& other) = delete; ScopedVector& operator=(const ScopedVector& other) = delete;
// Get an rvalue reference. (sv.Pass() does the same thing as std::move(sv).) // Get an rvalue reference. (sv.Pass() does the same thing as std::move(sv).)
ScopedVector&& Pass() { return static_cast<ScopedVector&&>(*this); } ScopedVector&& Pass() { return std::move(*this); }
reference operator[](size_t index) { return v_[index]; } reference operator[](size_t index) { return v_[index]; }
const_reference operator[](size_t index) const { return v_[index]; } const_reference operator[](size_t index) const { return v_[index]; }