From cd7e358a6b708aae1eb2d602933bcaf17865f349 Mon Sep 17 00:00:00 2001 From: Carson Radtke Date: Tue, 11 Feb 2025 13:45:06 -0600 Subject: [PATCH] introduce gsl::not_null::element_type --- 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..e11e7b6 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_v::element_type, int>, + "check member type: element_type"); +} + } // namespace diff --git a/tests/strict_notnull_tests.cpp b/tests/strict_notnull_tests.cpp index d75f5d6..e4022aa 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_v::element_type, int>, + "check member type: element_type"); +} + #if defined(__cplusplus) && (__cplusplus >= 201703L) TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)