Skip to content

Commit

Permalink
Bug 1466909 - Use AddLvalueReference for UniquePtr's operator*(). r=f…
Browse files Browse the repository at this point in the history
…roydnj
  • Loading branch information
anba committed Jun 7, 2018
1 parent 80245a6 commit cc1f99e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mfbt/UniquePtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class UniquePtr
return *this;
}

T& operator*() const { return *get(); }
typename AddLvalueReference<T>::Type operator*() const { return *get(); }
Pointer operator->() const
{
MOZ_ASSERT(get(), "dereferencing a UniquePtr containing nullptr");
Expand Down
35 changes: 35 additions & 0 deletions mfbt/tests/TestUniquePtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "mozilla/Move.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/Vector.h"

#include <stddef.h>
Expand All @@ -18,6 +19,7 @@ using mozilla::IsSame;
using mozilla::MakeUnique;
using mozilla::Swap;
using mozilla::UniquePtr;
using mozilla::UniqueFreePtr;
using mozilla::Vector;

#define CHECK(c) \
Expand Down Expand Up @@ -562,6 +564,36 @@ TestMakeUnique()
return true;
}

static bool
TestVoid()
{
// UniquePtr<void> supports all operations except operator*() and
// operator->().
UniqueFreePtr<void> p1(malloc(1));
UniqueFreePtr<void> p2;

auto x = p1.get();
CHECK(x != nullptr);
CHECK((IsSame<decltype(x), void*>::value));

p2.reset(p1.release());
CHECK(p1.get() == nullptr);
CHECK(p2.get() != nullptr);

p1 = std::move(p2);
CHECK(p1);
CHECK(!p2);

p1.swap(p2);
CHECK(!p1);
CHECK(p2);

p2 = nullptr;
CHECK(!p2);

return true;
}

int
main()
{
Expand All @@ -588,5 +620,8 @@ main()
if (!TestMakeUnique()) {
return 1;
}
if (!TestVoid()) {
return 1;
}
return 0;
}

0 comments on commit cc1f99e

Please sign in to comment.