Skip to content

Commit 9f9f65c

Browse files
maflckoMarcoFalke
andauthored
Mark gsl::not_null swap noexcept (#1235)
Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^[email protected]>
1 parent 5ca9c77 commit 9f9f65c

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

include/gsl/pointers

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ public:
145145
not_null& operator-=(std::ptrdiff_t) = delete;
146146
void operator[](std::ptrdiff_t) const = delete;
147147

148-
void swap(not_null<T>& other) { std::swap(ptr_, other.ptr_); }
148+
void swap(not_null<T>& other) noexcept { std::swap(ptr_, other.ptr_); }
149149

150150
private:
151151
T ptr_;
152152
};
153153

154154
template <typename T, std::enable_if_t<std::is_move_assignable<T>::value && std::is_move_constructible<T>::value, bool> = true>
155-
void swap(not_null<T>& a, not_null<T>& b)
155+
void swap(not_null<T>& a, not_null<T>& b) noexcept
156156
{
157157
a.swap(b);
158158
}

tests/pointers_tests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ TEST(pointers_test, swap)
4343
gsl::not_null<std::unique_ptr<int>> a(std::make_unique<int>(0));
4444
gsl::not_null<std::unique_ptr<int>> b(std::make_unique<int>(1));
4545

46+
static_assert(noexcept(gsl::swap(a, b)), "not null unique_ptr should be noexcept-swappable");
47+
4648
EXPECT_TRUE(*a == 0);
4749
EXPECT_TRUE(*b == 1);
4850

@@ -62,6 +64,8 @@ TEST(pointers_test, swap)
6264
gsl::strict_not_null<std::unique_ptr<int>> a{std::make_unique<int>(0)};
6365
gsl::strict_not_null<std::unique_ptr<int>> b{std::make_unique<int>(1)};
6466

67+
static_assert(noexcept(gsl::swap(a, b)), "strict not null unique_ptr should be noexcept-swappable");
68+
6569
EXPECT_TRUE(*a == 0);
6670
EXPECT_TRUE(*b == 1);
6771

0 commit comments

Comments
 (0)