Merge pull request #87 from trebconnell/fixmaybenull

Fix issue #50
This commit is contained in:
Neil MacIntosh 2015-09-28 18:40:57 -07:00
commit 318cee0489
2 changed files with 14 additions and 3 deletions

View File

@ -178,6 +178,9 @@ class maybe_null_ret;
template<class T> template<class T>
class maybe_null_dbg class maybe_null_dbg
{ {
template<class U>
friend class maybe_null_dbg;
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr."); static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
public: public:
maybe_null_dbg() : ptr_(nullptr), tested_(false) {} maybe_null_dbg() : ptr_(nullptr), tested_(false) {}
@ -247,8 +250,10 @@ public:
bool operator==(const T& rhs) const { tested_ = true; return ptr_ == rhs; } bool operator==(const T& rhs) const { tested_ = true; return ptr_ == rhs; }
bool operator!=(const T& rhs) const { return !(*this == rhs); } bool operator!=(const T& rhs) const { return !(*this == rhs); }
bool operator==(const maybe_null_dbg& rhs) const { tested_ = true; rhs.tested_ = true; return ptr_ == rhs.ptr_; } template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
bool operator!=(const maybe_null_dbg& rhs) const { return !(*this == rhs); } bool operator==(const maybe_null_dbg<U>& rhs) const { tested_ = true; rhs.tested_ = true; return ptr_ == rhs.ptr_; }
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
bool operator!=(const maybe_null_dbg<U>& rhs) const { return !(*this == rhs); }
T get() const { T get() const {
fail_fast_assert(tested_); fail_fast_assert(tested_);

View File

@ -178,13 +178,19 @@ SUITE(MaybeNullTests)
maybe_null<MyBase*> q = p; maybe_null<MyBase*> q = p;
CHECK(q == p); CHECK(q == p);
maybe_null_dbg<MyDerived*> pdbg = &derived;
CHECK(pdbg.present());
maybe_null_dbg<MyBase*> qdbg = pdbg;
CHECK(qdbg == pdbg);
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
maybe_null<Unrelated*> r = p; maybe_null<Unrelated*> r = p;
maybe_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p); maybe_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
#endif #endif
maybe_null_dbg<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get()); maybe_null_dbg<Unrelated*> t = reinterpret_cast<Unrelated*>(p.get());
CHECK_THROW((void)(void*)t.get(), fail_fast); CHECK_THROW((void)(void*)t.get(), fail_fast);
maybe_null_dbg<Unrelated*> u = reinterpret_cast<Unrelated*>(p.get()); maybe_null_dbg<Unrelated*> u = reinterpret_cast<Unrelated*>(p.get());
CHECK(u.present()); CHECK(u.present());
CHECK((void*)p.get() == (void*)u.get()); CHECK((void*)p.get() == (void*)u.get());