Skip to content

Commit ec729d6

Browse files
authored
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
1 parent 7f4fc93 commit ec729d6

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

docs/headers.md

+8
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ When a nullptr check fails, `std::terminate` is called.
224224

225225
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)
226226

227+
#### Member Types
228+
229+
```cpp
230+
using element_type = T;
231+
```
232+
233+
The type of the pointed-to object.
234+
227235
#### Member functions
228236

229237
##### Construct/Copy

include/gsl/pointers

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class not_null
9898
public:
9999
static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");
100100

101+
using element_type = T;
102+
101103
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
102104
constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : ptr_(std::forward<U>(u))
103105
{

tests/pointers_tests.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,10 @@ TEST(pointers_test, swap)
8888
"!SwapCompilesFor<NotMoveAssignableCustomPtr>");
8989
}
9090

91+
TEST(pointers_test, member_types)
92+
{
93+
static_assert(std::is_same<gsl::not_null<int*>::element_type, int*>::value,
94+
"check member type: element_type");
95+
}
96+
9197
} // namespace

tests/strict_notnull_tests.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,13 @@ TEST(strict_notnull_tests, TestStrictNotNull)
366366
}
367367
}
368368

369+
TEST(pointers_test, member_types)
370+
{
371+
// make sure `element_type` is inherited from `gsl::not_null`
372+
static_assert(std::is_same<gsl::strict_not_null<int*>::element_type, int*>::value,
373+
"check member type: element_type");
374+
}
375+
369376
#if defined(__cplusplus) && (__cplusplus >= 201703L)
370377

371378
TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)

0 commit comments

Comments
 (0)