introduce gsl::swap for swapping gsl::not_null (#1160)

fixes: https://github.com/microsoft/GSL/issues/1129

* create gsl::swap<T>(T&, T&) which wraps std::swap
* specialize gsl::swap<T>(gsl::not_null<T>&, gsl::not_null<T>&)
* add tests
This commit is contained in:
Carson Radtke
2024-12-13 15:16:53 -06:00
committed by GitHub
parent 8a0e3d8a9b
commit 272463043e
5 changed files with 53 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
#include <gtest/gtest.h>
#include <gsl/pointers>
#include <memory>
TEST(pointers_test, swap)
{
// taken from gh-1129:
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));
EXPECT_TRUE(*a == 0);
EXPECT_TRUE(*b == 1);
gsl::swap(a, b);
EXPECT_TRUE(*a == 1);
EXPECT_TRUE(*b == 0);
}