Allow dereferencing rvalues in unique_ref

This commit is contained in:
Sebastian Messmer 2015-06-28 17:34:23 +02:00
parent 72cb9fa038
commit 6442512877
2 changed files with 25 additions and 5 deletions

View File

@ -36,9 +36,12 @@ public:
return *this;
}
typename std::add_lvalue_reference<T>::type operator*() const {
typename std::add_lvalue_reference<T>::type operator*() const& {
return *_target;
}
typename std::add_rvalue_reference<T>::type operator*() && {
return std::move(*_target);
}
T* operator->() const {
return get();

View File

@ -89,22 +89,22 @@ TEST(NullcheckTest, ClassWith2Parameters) {
TEST(NullcheckTest, OptionIsResolvable_Primitive) {
boost::optional<unique_ref<int>> var = nullcheck(std::make_unique<int>(3));
unique_ref<int> resolved = std::move(*var);
unique_ref<int> resolved = std::move(var).value();
}
TEST(NullcheckTest, OptionIsResolvable_Object) {
boost::optional<unique_ref<SomeClass0Parameters>> var = nullcheck(std::make_unique<SomeClass>());
unique_ref<SomeClass0Parameters> resolved = std::move(*var);
unique_ref<SomeClass0Parameters> resolved = std::move(var).value();
}
TEST(NullcheckTest, OptionIsAutoResolvable_Primitive) {
auto var = nullcheck(std::make_unique<int>(3));
auto resolved = std::move(*var);
auto resolved = std::move(var).value();
}
TEST(NullcheckTest, OptionIsAutoResolvable_Object) {
auto var = nullcheck(std::make_unique<SomeClass>());
auto resolved = std::move(*var);
auto resolved = std::move(var).value();
}
class UniqueRefTest: public ::testing::Test {
@ -400,3 +400,20 @@ TEST_F(UniqueRefTest, NullptrIsNotLessThanNullptr) {
makeInvalid(std::move(var2));
EXPECT_FALSE(std::less<unique_ref<int>>()(var1, var2));
}
class OnlyMoveable {
public:
OnlyMoveable(int value_): value(value_) {}
OnlyMoveable(OnlyMoveable &&source): value(source.value) {source.value = -1;}
bool operator==(const OnlyMoveable &rhs) const {
return value == rhs.value;
}
int value;
private:
DISALLOW_COPY_AND_ASSIGN(OnlyMoveable);
};
TEST_F(UniqueRefTest, AllowsDerefOnRvalue) {
OnlyMoveable val = *make_unique_ref<OnlyMoveable>(5);
EXPECT_EQ(OnlyMoveable(5), val);
}