From 23581d4d63e6ed41c9e3a3886f24d526dd7a557d Mon Sep 17 00:00:00 2001 From: saurabh singh Date: Tue, 30 May 2017 22:36:21 +0530 Subject: [PATCH] Adding derference operator to not_null (#513) --- include/gsl/gsl | 1 + tests/notnull_tests.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/gsl/gsl b/include/gsl/gsl index e6df917..65ce7eb 100644 --- a/include/gsl/gsl +++ b/include/gsl/gsl @@ -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; diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index bb043bb..85ed5b5 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -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(42); + + using NotNullSp1 = not_null; + + CHECK(*NotNullSp1(sp1) == *sp1); + + int ints[1] = {42}; + CustomPtr p1(&ints[0]); + + using NotNull1 = not_null; + CHECK(*NotNull1(p1) == 42); + } } int main(int, const char* []) { return UnitTest::RunAllTests(); }