diff --git a/include/gsl.h b/include/gsl.h index bf3573e..1405993 100644 --- a/include/gsl.h +++ b/include/gsl.h @@ -178,6 +178,9 @@ class maybe_null_ret; template class maybe_null_dbg { + template + friend class maybe_null_dbg; + static_assert(std::is_assignable::value, "T cannot be assigned nullptr."); public: 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 { return !(*this == rhs); } - bool operator==(const maybe_null_dbg& rhs) const { tested_ = true; rhs.tested_ = true; return ptr_ == rhs.ptr_; } - bool operator!=(const maybe_null_dbg& rhs) const { return !(*this == rhs); } + template ::value>> + bool operator==(const maybe_null_dbg& rhs) const { tested_ = true; rhs.tested_ = true; return ptr_ == rhs.ptr_; } + template ::value>> + bool operator!=(const maybe_null_dbg& rhs) const { return !(*this == rhs); } T get() const { fail_fast_assert(tested_); diff --git a/tests/maybenull_tests.cpp b/tests/maybenull_tests.cpp index d6b53f0..944307e 100644 --- a/tests/maybenull_tests.cpp +++ b/tests/maybenull_tests.cpp @@ -178,13 +178,19 @@ SUITE(MaybeNullTests) maybe_null q = p; CHECK(q == p); + maybe_null_dbg pdbg = &derived; + CHECK(pdbg.present()); + + maybe_null_dbg qdbg = pdbg; + CHECK(qdbg == pdbg); + #ifdef CONFIRM_COMPILATION_ERRORS maybe_null r = p; maybe_null s = reinterpret_cast(p); #endif maybe_null_dbg t = reinterpret_cast(p.get()); - CHECK_THROW((void)(void*)t.get(), fail_fast); + CHECK_THROW((void)(void*)t.get(), fail_fast); maybe_null_dbg u = reinterpret_cast(p.get()); CHECK(u.present()); CHECK((void*)p.get() == (void*)u.get());