introduce gsl::not_null<T>::element_type

This commit is contained in:
Carson Radtke 2025-02-11 13:45:06 -06:00
parent 355982daf6
commit cd7e358a6b
4 changed files with 23 additions and 0 deletions

View File

@ -224,6 +224,14 @@ When a nullptr check fails, `std::terminate` is called.
See [F.23: Use a `not_null<T>` 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

View File

@ -98,6 +98,8 @@ class not_null
public:
static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");
using element_type = T;
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : ptr_(std::forward<U>(u))
{

View File

@ -88,4 +88,10 @@ TEST(pointers_test, swap)
"!SwapCompilesFor<NotMoveAssignableCustomPtr>");
}
TEST(pointers_test, member_types)
{
static_assert(std::is_same_v<gsl::not_null<int>::element_type, int>,
"check member type: element_type");
}
} // namespace

View File

@ -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<gsl::strict_not_null<int>::element_type, int>,
"check member type: element_type");
}
#if defined(__cplusplus) && (__cplusplus >= 201703L)
TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)