From ec729d63a76ff97326de6975711fa960bfcd3ce2 Mon Sep 17 00:00:00 2001 From: Carson Radtke Date: Wed, 12 Feb 2025 09:16:05 -0600 Subject: [PATCH] introduce gsl::not_null::element_type (#1196) * introduce gsl::not_null::element_type * use std::is_same instead of is_same_v * fix: cannot put a non-pointer in a gsl::not_null --- docs/headers.md | 8 ++++++++ include/gsl/pointers | 2 ++ tests/pointers_tests.cpp | 6 ++++++ tests/strict_notnull_tests.cpp | 7 +++++++ 4 files changed, 23 insertions(+) diff --git a/docs/headers.md b/docs/headers.md index 96465a3..ff7ec31 100644 --- a/docs/headers.md +++ b/docs/headers.md @@ -224,6 +224,14 @@ When a nullptr check fails, `std::terminate` is called. See [F.23: Use a `not_null` to indicate that “null” is not a valid value](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-nullptr) +#### Member Types + +```cpp +using element_type = T; +``` + +The type of the pointed-to object. + #### Member functions ##### Construct/Copy diff --git a/include/gsl/pointers b/include/gsl/pointers index e1bda04..af631b9 100644 --- a/include/gsl/pointers +++ b/include/gsl/pointers @@ -98,6 +98,8 @@ class not_null public: static_assert(details::is_comparable_to_nullptr::value, "T cannot be compared to nullptr."); + using element_type = T; + template ::value>> constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible::value) : ptr_(std::forward(u)) { diff --git a/tests/pointers_tests.cpp b/tests/pointers_tests.cpp index 1c15712..729cc5f 100644 --- a/tests/pointers_tests.cpp +++ b/tests/pointers_tests.cpp @@ -88,4 +88,10 @@ TEST(pointers_test, swap) "!SwapCompilesFor"); } +TEST(pointers_test, member_types) +{ + static_assert(std::is_same::element_type, int*>::value, + "check member type: element_type"); +} + } // namespace diff --git a/tests/strict_notnull_tests.cpp b/tests/strict_notnull_tests.cpp index d75f5d6..0626d8f 100644 --- a/tests/strict_notnull_tests.cpp +++ b/tests/strict_notnull_tests.cpp @@ -366,6 +366,13 @@ TEST(strict_notnull_tests, TestStrictNotNull) } } +TEST(pointers_test, member_types) +{ + // make sure `element_type` is inherited from `gsl::not_null` + static_assert(std::is_same::element_type, int*>::value, + "check member type: element_type"); +} + #if defined(__cplusplus) && (__cplusplus >= 201703L) TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)