Skip to content

Commit fa06ffc

Browse files
author
MarcoFalke
committed
Mark gsl::not_null swap noexcept when possible
1 parent bf9d5e1 commit fa06ffc

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

include/gsl/pointers

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,17 @@ 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(noexcept(std::swap(std::declval<T&>(), std::declval<T&>())))
149+
{
150+
std::swap(ptr_, other.ptr_);
151+
}
149152

150153
private:
151154
T ptr_;
152155
};
153156

154157
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)
158+
void swap(not_null<T>& a, not_null<T>& b) noexcept(noexcept(a.swap(b)))
156159
{
157160
a.swap(b);
158161
}

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)