Adding derference operator to not_null (#513)

This commit is contained in:
saurabh singh 2017-05-30 22:36:21 +05:30 committed by Neil MacIntosh
parent d1032864aa
commit 23581d4d63
2 changed files with 16 additions and 0 deletions

View File

@ -98,6 +98,7 @@ public:
constexpr operator T() const { return get(); }
constexpr T operator->() const { return get(); }
constexpr auto operator*() const { return *get(); }
// prevents compilation when someone attempts to assign a null pointer constant
not_null(std::nullptr_t) = delete;

View File

@ -250,6 +250,21 @@ SUITE(NotNullTests)
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
}
TEST(TestNotNullDereferenceOperator)
{
auto sp1 = std::make_shared<int>(42);
using NotNullSp1 = not_null<decltype(sp1)>;
CHECK(*NotNullSp1(sp1) == *sp1);
int ints[1] = {42};
CustomPtr<int> p1(&ints[0]);
using NotNull1 = not_null<decltype(p1)>;
CHECK(*NotNull1(p1) == 42);
}
}
int main(int, const char* []) { return UnitTest::RunAllTests(); }