From 40009ff6eb6dc6bc533675f836509c3b791a6730 Mon Sep 17 00:00:00 2001 From: Kern Handa Date: Fri, 25 Sep 2015 09:41:40 -0700 Subject: [PATCH] not_null and maybe_null variants should only work on pointer types. --- include/gsl.h | 3 +++ tests/maybenull_tests.cpp | 5 +++++ tests/notnull_tests.cpp | 2 ++ 3 files changed, 10 insertions(+) diff --git a/include/gsl.h b/include/gsl.h index 1357d76..c7e7fcb 100644 --- a/include/gsl.h +++ b/include/gsl.h @@ -102,6 +102,7 @@ typename Cont::value_type& at(Cont& cont, size_t index) { fail_fast_assert(index template class not_null { + static_assert(std::is_pointer::value, "T must be a pointer type"); public: not_null(T t) : ptr_(t) { ensure_invariant(); } @@ -162,6 +163,7 @@ private: template class maybe_null_dbg { + static_assert(std::is_pointer::value, "T must be a pointer type"); public: maybe_null_dbg() : ptr_(nullptr), tested_(false) {} @@ -237,6 +239,7 @@ private: template class maybe_null_ret { + static_assert(std::is_pointer::value, "T must be a pointer type"); public: maybe_null_ret() : ptr_(nullptr) {} maybe_null_ret(std::nullptr_t) : ptr_(nullptr) {} diff --git a/tests/maybenull_tests.cpp b/tests/maybenull_tests.cpp index 0a9d891..10ac6b5 100644 --- a/tests/maybenull_tests.cpp +++ b/tests/maybenull_tests.cpp @@ -16,6 +16,7 @@ #include #include +#include using namespace Guide; @@ -27,6 +28,10 @@ SUITE(MaybeNullTests) { TEST(TestMaybeNull1) { +#ifdef CONFIRM_COMPILATION_ERRORS + maybe_null_dbg> f(std::vector{1}); // Not a pointer type. Must be tested with a non-integral type. + maybe_null_ret> g(std::vector{1}); // Not a pointer type. Must be tested with a non-integral type. +#endif int n = 5; maybe_null_dbg opt_n(&n); int result = 0; diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index 008cbb3..bf3e0e7 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -16,6 +16,7 @@ #include #include +#include using namespace Guide; @@ -48,6 +49,7 @@ SUITE(NotNullTests) not_null p; // yay...does not compile! std::unique_ptr up = std::make_unique(120); not_null p = up; + not_null> f(std::vector{1}); // Not a pointer type. Must be tested with a non-integral type. #endif int i = 12; auto rp = RefCounted(&i);