Mark gsl::not_null swap noexcept (#1235)

Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
This commit is contained in:
maflcko
2026-03-23 12:16:36 -06:00
committed by GitHub
co-authored by MarcoFalke
parent 5ca9c77666
commit 9f9f65c7e2
2 changed files with 6 additions and 2 deletions
+2 -2
View File
@@ -145,14 +145,14 @@ public:
not_null& operator-=(std::ptrdiff_t) = delete;
void operator[](std::ptrdiff_t) const = delete;
void swap(not_null<T>& other) { std::swap(ptr_, other.ptr_); }
void swap(not_null<T>& other) noexcept { std::swap(ptr_, other.ptr_); }
private:
T ptr_;
};
template <typename T, std::enable_if_t<std::is_move_assignable<T>::value && std::is_move_constructible<T>::value, bool> = true>
void swap(not_null<T>& a, not_null<T>& b)
void swap(not_null<T>& a, not_null<T>& b) noexcept
{
a.swap(b);
}
+4
View File
@@ -43,6 +43,8 @@ TEST(pointers_test, swap)
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));
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<std::unique_ptr<int>> a{std::make_unique<int>(0)};
gsl::strict_not_null<std::unique_ptr<int>> b{std::make_unique<int>(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);