introduce gsl::not_null<T>::element_type (#1196)

* introduce gsl::not_null<T>::element_type

* use std::is_same instead of is_same_v

* fix: cannot put a non-pointer in a gsl::not_null
This commit is contained in:
Carson Radtke 2025-02-12 09:16:05 -06:00 committed by GitHub
parent 7f4fc9388b
commit ec729d63a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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<gsl::not_null<int*>::element_type, int*>::value,
"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<gsl::strict_not_null<int*>::element_type, int*>::value,
"check member type: element_type");
}
#if defined(__cplusplus) && (__cplusplus >= 201703L)
TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)