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:
@ -172,7 +172,7 @@ class Buffer {
|
||||
// b.Pass() does the same thing as std::move(b).
|
||||
Buffer&& Pass() {
|
||||
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
|
||||
|
@ -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<T&&>(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<T&&>(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<T&&>(m.value_);
|
||||
value_ = std::move(m.value_);
|
||||
has_value_ = m.has_value_;
|
||||
return *this;
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ TEST(OptionalTest, TestConstructMoveEmpty) {
|
||||
{
|
||||
Optional<Logger> x;
|
||||
EXPECT_FALSE(x);
|
||||
auto y = static_cast<Optional<Logger>&&>(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<Logger> x(Logger(17));
|
||||
EXPECT_TRUE(x);
|
||||
log->push_back("---");
|
||||
auto y = static_cast<Optional<Logger>&&>(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<Logger> x, y;
|
||||
x = static_cast<Optional<Logger>&&>(y);
|
||||
x = std::move(y);
|
||||
}
|
||||
EXPECT_EQ(
|
||||
V("0:0. default constructor", "1:1. default constructor",
|
||||
@ -303,7 +303,7 @@ TEST(OptionalTest, TestMoveAssignToFullFromEmpty) {
|
||||
Optional<Logger> x(Logger(17));
|
||||
Optional<Logger> y;
|
||||
log->push_back("---");
|
||||
x = static_cast<Optional<Logger>&&>(y);
|
||||
x = std::move(y);
|
||||
log->push_back("---");
|
||||
}
|
||||
EXPECT_EQ(
|
||||
@ -320,7 +320,7 @@ TEST(OptionalTest, TestMoveAssignToEmptyFromFull) {
|
||||
Optional<Logger> x;
|
||||
Optional<Logger> y(Logger(17));
|
||||
log->push_back("---");
|
||||
x = static_cast<Optional<Logger>&&>(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<Logger> x(Logger(17));
|
||||
Optional<Logger> y(Logger(42));
|
||||
log->push_back("---");
|
||||
x = static_cast<Optional<Logger>&&>(y);
|
||||
x = std::move(y);
|
||||
log->push_back("---");
|
||||
}
|
||||
EXPECT_EQ(
|
||||
@ -354,7 +354,7 @@ TEST(OptionalTest, TestMoveAssignToEmptyFromT) {
|
||||
Optional<Logger> x;
|
||||
Logger y(17);
|
||||
log->push_back("---");
|
||||
x = Optional<Logger>(static_cast<Logger&&>(y));
|
||||
x = Optional<Logger>(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<Logger> x(Logger(17));
|
||||
Logger y(42);
|
||||
log->push_back("---");
|
||||
x = Optional<Logger>(static_cast<Logger&&>(y));
|
||||
x = Optional<Logger>(std::move(y));
|
||||
log->push_back("---");
|
||||
}
|
||||
EXPECT_EQ(
|
||||
@ -390,13 +390,13 @@ TEST(OptionalTest, TestDereference) {
|
||||
log->push_back("---");
|
||||
x->Foo();
|
||||
y->Foo();
|
||||
static_cast<Optional<Logger>&&>(x)->Foo();
|
||||
static_cast<const Optional<Logger>&&>(y)->Foo();
|
||||
std::move(x)->Foo();
|
||||
std::move(y)->Foo();
|
||||
log->push_back("---");
|
||||
(*x).Foo();
|
||||
(*y).Foo();
|
||||
(*static_cast<Optional<Logger>&&>(x)).Foo();
|
||||
(*static_cast<const Optional<Logger>&&>(y)).Foo();
|
||||
(*std::move(x)).Foo();
|
||||
(*std::move(y)).Foo();
|
||||
log->push_back("---");
|
||||
}
|
||||
EXPECT_EQ(V("0:42. explicit constructor",
|
||||
|
@ -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<scoped_ptr&&>(*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<T[], D> {
|
||||
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<scoped_ptr&&>(*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.
|
||||
|
@ -43,9 +43,7 @@ class ScopedVector {
|
||||
~ScopedVector() { clear(); }
|
||||
|
||||
// Move construction and assignment.
|
||||
ScopedVector(ScopedVector&& other) {
|
||||
*this = static_cast<ScopedVector&&>(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<ScopedVector&&>(*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]; }
|
||||
|
Reference in New Issue
Block a user