From fafa5399f30f851787af9a409db0852001a8f34b Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 19 Mar 2026 19:36:03 +0100 Subject: [PATCH] Mark gsl::not_null swap noexcept --- include/gsl/pointers | 4 ++-- tests/pointers_tests.cpp | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/gsl/pointers b/include/gsl/pointers index d73dc05de..c6a5ae88d 100644 --- a/include/gsl/pointers +++ b/include/gsl/pointers @@ -145,14 +145,14 @@ public: not_null& operator-=(std::ptrdiff_t) = delete; void operator[](std::ptrdiff_t) const = delete; - void swap(not_null& other) { std::swap(ptr_, other.ptr_); } + void swap(not_null& other) noexcept { std::swap(ptr_, other.ptr_); } private: T ptr_; }; template ::value && std::is_move_constructible::value, bool> = true> -void swap(not_null& a, not_null& b) +void swap(not_null& a, not_null& b) noexcept { a.swap(b); } diff --git a/tests/pointers_tests.cpp b/tests/pointers_tests.cpp index 729cc5f0d..6e72bf7cf 100644 --- a/tests/pointers_tests.cpp +++ b/tests/pointers_tests.cpp @@ -43,6 +43,8 @@ TEST(pointers_test, swap) gsl::not_null> a(std::make_unique(0)); gsl::not_null> b(std::make_unique(1)); + static_assert(noexcept(gsl::swap(a, b)), "not null unique_ptr should be noexcept-swappable"); + EXPECT_TRUE(*a == 0); EXPECT_TRUE(*b == 1); @@ -62,6 +64,8 @@ TEST(pointers_test, swap) gsl::strict_not_null> a{std::make_unique(0)}; gsl::strict_not_null> b{std::make_unique(1)}; + static_assert(noexcept(gsl::swap(a, b)), "strict not null unique_ptr should be noexcept-swappable"); + EXPECT_TRUE(*a == 0); EXPECT_TRUE(*b == 1);