Skip to content

gsl::not_null: C.85: Make swap noexcept#1235

Merged
carsonRadtke merged 1 commit intomicrosoft:mainfrom
maflcko:2603-swap
Mar 23, 2026
Merged

gsl::not_null: C.85: Make swap noexcept#1235
carsonRadtke merged 1 commit intomicrosoft:mainfrom
maflcko:2603-swap

Conversation

@maflcko
Copy link
Copy Markdown
Contributor

@maflcko maflcko commented Mar 19, 2026

Currently, std::is_nothrow_swappable_v<gsl::not_null<std::shared_ptr<int>>> fails. This violates https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c85-make-swap-noexcept

So mark not_null with the appropriate noexcept.

This can be tested by compiling the test without the include header changes and observing the failure:

tests/pointers_tests.cpp:46:23: error: static assertion failed due to requirement 'noexcept(gsl::swap(a, b))': not null unique_ptr should be noexcept-swappable
   46 |         static_assert(noexcept(gsl::swap(a, b)), "not null unique_ptr should be noexcept-swappable");
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~
tests/pointers_tests.cpp:67:23: error: static assertion failed due to requirement 'noexcept(gsl::swap(a, b))': strict not null unique_ptr should be noexcept-swappable
   67 |         static_assert(noexcept(gsl::swap(a, b)), "strict not null unique_ptr should be noexcept-swappable");
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~

Similarly, clang-tidy will complain on the previous header:

$ clang-tidy --checks='performance-noexcept-swap' -p ./build/clang-23-release/ ./include/gsl/pointers 
include/gsl/pointers:148:10: warning: swap functions should be marked noexcept [performance-noexcept-swap]
  148 |     void swap(not_null<T>& other) { std::swap(ptr_, other.ptr_); }

include/gsl/pointers:155:6: warning: swap functions should be marked noexcept [performance-noexcept-swap]
  155 | void swap(not_null<T>& a, not_null<T>& b)

@maflcko maflcko force-pushed the 2603-swap branch 6 times, most recently from fa833b2 to fa06ffc Compare March 19, 2026 19:03
@carsonRadtke
Copy link
Copy Markdown
Member

Thanks for finding this issue! Can we make this unconditionally noexcept?

C.85: Make swap noexcept

@maflcko maflcko changed the title Mark gsl::not_null swap noexcept when possible Mark gsl::not_null swap noexcept Mar 20, 2026
@maflcko maflcko changed the title Mark gsl::not_null swap noexcept gsl::not_null: C.85: Make swap noexcept Mar 20, 2026
@maflcko
Copy link
Copy Markdown
Contributor Author

maflcko commented Mar 20, 2026

Thanks for finding this issue! Can we make this unconditionally noexcept?

C.85: Make swap noexcept

Heh, thanks for the link. I should have read the docs myself, because a plain noexecpt is also the easiest to implement.

@maflcko
Copy link
Copy Markdown
Contributor Author

maflcko commented Mar 23, 2026

I think the CI had a hiccup, because I can't see the log. Let's try again... (edit: CI looks better)

@carsonRadtke
Copy link
Copy Markdown
Member

Thanks!

@carsonRadtke carsonRadtke merged commit 9f9f65c into microsoft:main Mar 23, 2026
87 checks passed
@maflcko maflcko deleted the 2603-swap branch March 24, 2026 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants