strict_not_null for unique_ptr (#1179)
Some checks failed
CI_Android / Android (push) Has been cancelled
cmake_find_package / Build ${{ matrix.os }} (macos-latest) (push) Has been cancelled
cmake_find_package / Build ${{ matrix.os }} (ubuntu-latest) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 11) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 23) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled

- `strict_not_null<std::unique_ptr<int>>{ std::make_unique<int>()}` failed to compile
  - `strict_not_null` ctor needs to move the passed `unique_ptr`, not copy
  - Copied `not_null` `TestNotNullConstructors` for `strict_not_null`
- The `noexcept` specifiers on the `strict_not_null` and `not_null` constructors were out of sync.
- Added unit test for `not_null<unique_ptr<T>>` and for `strict_not_null<unique_ptr<T>>`
- Added unit test for `gsl::swap` for two `strict_not_null`
- Added unit test for `gsl::swap` for `not_null` and `strict_not_null`

Co-authored-by: Werner Henze <w.henze@avm.de>
This commit is contained in:
Werner Henze 2024-12-26 18:00:36 +01:00 committed by GitHub
parent fcd55ee924
commit 16a60199df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 166 additions and 17 deletions

View File

@ -275,19 +275,19 @@ class strict_not_null : public not_null<T>
{ {
public: public:
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>> template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr explicit strict_not_null(U&& u) : not_null<T>(std::forward<U>(u)) constexpr explicit strict_not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null<T>(std::forward<U>(u))
{} {}
template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>> template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
constexpr explicit strict_not_null(T u) : not_null<T>(u) constexpr explicit strict_not_null(T u) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null<T>(std::move(u))
{} {}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>> template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr strict_not_null(const not_null<U>& other) : not_null<T>(other) constexpr strict_not_null(const not_null<U>& other) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null<T>(other)
{} {}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>> template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr strict_not_null(const strict_not_null<U>& other) : not_null<T>(other) constexpr strict_not_null(const strict_not_null<U>& other) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null<T>(other)
{} {}
// To avoid invalidating the "not null" invariant, the contained pointer is actually copied // To avoid invalidating the "not null" invariant, the contained pointer is actually copied

View File

@ -178,6 +178,14 @@ TEST(notnull_tests, TestNotNullConstructors)
EXPECT_DEATH((not_null<decltype(pi)>(pi)), expected); EXPECT_DEATH((not_null<decltype(pi)>(pi)), expected);
} }
{
// from unique pointer
not_null<std::unique_ptr<int>> x(
std::make_unique<int>(10)); // unique_ptr<int> is nullptr assignable
EXPECT_DEATH((not_null<std::unique_ptr<int>>(std::unique_ptr<int>{})), expected);
}
{ {
// from pointer to local // from pointer to local
int t = 42; int t = 42;

View File

@ -25,6 +25,7 @@ struct NotMoveAssignableCustomPtr
TEST(pointers_test, swap) TEST(pointers_test, swap)
{ {
// taken from gh-1129: // taken from gh-1129:
{
gsl::not_null<std::unique_ptr<int>> a(std::make_unique<int>(0)); gsl::not_null<std::unique_ptr<int>> a(std::make_unique<int>(0));
gsl::not_null<std::unique_ptr<int>> b(std::make_unique<int>(1)); gsl::not_null<std::unique_ptr<int>> b(std::make_unique<int>(1));
@ -41,6 +42,33 @@ TEST(pointers_test, swap)
const auto shared_custom_ptr{std::make_shared<NotMoveAssignableCustomPtr>()}; const auto shared_custom_ptr{std::make_shared<NotMoveAssignableCustomPtr>()};
gsl::not_null<NotMoveAssignableCustomPtr> c{*shared_custom_ptr}; gsl::not_null<NotMoveAssignableCustomPtr> c{*shared_custom_ptr};
EXPECT_TRUE(c.get() != nullptr); EXPECT_TRUE(c.get() != nullptr);
}
{
gsl::strict_not_null<std::unique_ptr<int>> a{std::make_unique<int>(0)};
gsl::strict_not_null<std::unique_ptr<int>> b{std::make_unique<int>(1)};
EXPECT_TRUE(*a == 0);
EXPECT_TRUE(*b == 1);
gsl::swap(a, b);
EXPECT_TRUE(*a == 1);
EXPECT_TRUE(*b == 0);
}
{
gsl::not_null<std::unique_ptr<int>> a{std::make_unique<int>(0)};
gsl::strict_not_null<std::unique_ptr<int>> b{std::make_unique<int>(1)};
EXPECT_TRUE(*a == 0);
EXPECT_TRUE(*b == 1);
gsl::swap(a, b);
EXPECT_TRUE(*a == 1);
EXPECT_TRUE(*b == 0);
}
} }
#if __cplusplus >= 201703l #if __cplusplus >= 201703l

View File

@ -21,6 +21,15 @@
using namespace gsl; using namespace gsl;
// stand-in for a user-defined ref-counted class
template <typename T>
struct RefCounted
{
RefCounted(T* p) : p_(p) {}
operator T*() { return p_; }
T* p_;
};
namespace namespace
{ {
// clang-format off // clang-format off
@ -43,12 +52,116 @@ GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
// clang-format on // clang-format on
bool strict_helper_const(strict_not_null<const int*> p) { return *p == 12; } bool strict_helper_const(strict_not_null<const int*> p) { return *p == 12; }
#ifdef CONFIRM_COMPILATION_ERRORS
int* return_pointer() { return nullptr; } int* return_pointer() { return nullptr; }
#ifdef CONFIRM_COMPILATION_ERRORS
const int* return_pointer_const() { return nullptr; } const int* return_pointer_const() { return nullptr; }
#endif #endif
} // namespace } // namespace
TEST(strict_notnull_tests, TestStrictNotNullConstructors)
{
{
#ifdef CONFIRM_COMPILATION_ERRORS
strict_not_null<int*> p = nullptr; // yay...does not compile!
strict_not_null<std::vector<char>*> p1 = 0; // yay...does not compile!
strict_not_null<int*> p2; // yay...does not compile!
std::unique_ptr<int> up = std::make_unique<int>(120);
strict_not_null<int*> p3 = up;
// Forbid non-nullptr assignable types
strict_not_null<std::vector<int>> f(std::vector<int>{1});
strict_not_null<int> z(10);
strict_not_null<std::vector<int>> y({1, 2});
#endif
}
const auto terminateHandler = std::set_terminate([] {
std::cerr << "Expected Death. TestNotNullConstructors";
std::abort();
});
const auto expected = GetExpectedDeathString(terminateHandler);
{
// from shared pointer
int i = 12;
auto rp = RefCounted<int>(&i);
strict_not_null<int*> p(rp);
EXPECT_TRUE(p.get() == &i);
strict_not_null<std::shared_ptr<int>> x(
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
int* pi = nullptr;
EXPECT_DEATH((strict_not_null<decltype(pi)>(pi)), expected);
}
{
// from unique pointer
strict_not_null<std::unique_ptr<int>> x(
std::make_unique<int>(10)); // unique_ptr<int> is nullptr assignable
EXPECT_DEATH((strict_not_null<std::unique_ptr<int>>(std::unique_ptr<int>{})), expected);
}
{
// from pointer to local
int t = 42;
strict_not_null<int*> x{&t};
helper(&t);
helper_const(&t);
EXPECT_TRUE(*x == 42);
}
{
// from raw pointer
// from strict_not_null pointer
int t = 42;
int* p = &t;
strict_not_null<int*> x{p};
helper(p);
helper_const(p);
helper(x);
helper_const(x);
EXPECT_TRUE(*x == 42);
}
{
// from raw const pointer
// from strict_not_null const pointer
int t = 42;
const int* cp = &t;
strict_not_null<const int*> x{cp};
helper_const(cp);
helper_const(x);
EXPECT_TRUE(*x == 42);
}
{
// from strict_not_null const pointer, using auto
int t = 42;
const int* cp = &t;
auto x = strict_not_null<const int*>{cp};
EXPECT_TRUE(*x == 42);
}
{
// from returned pointer
EXPECT_DEATH(helper(return_pointer()), expected);
EXPECT_DEATH(helper_const(return_pointer()), expected);
}
}
TEST(strict_notnull_tests, TestStrictNotNull) TEST(strict_notnull_tests, TestStrictNotNull)
{ {
{ {