[libcxx-commits] [libcxx] [libc++] Optimize std::swap of locales (PR #209760)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jul 29 19:59:22 PDT 2026


================
@@ -143,8 +143,14 @@ private:
   friend bool has_facet(const locale&) _NOEXCEPT;
   template <class _Facet>
   friend const _Facet& use_facet(const locale&);
+
+  friend void swap(locale&, locale&);
 };
 
+inline void swap(locale& __lhs, locale& __rhs) {
+  std::swap(__lhs.__locale_, __rhs.__locale_);
+}
----------------
frederick-vs-ja wrote:

We should add `noexcept`. This is required and libc++ used to do the right thing ([Godbolt link](https://godbolt.org/z/6MnKW3j1T)).

It can be deduced that `is_nothrow_swappable_v<locale>` is `true` from the standard wording - the swapping operation uses copy constructor and copy assignment operators of `locale`, which are all `noexcept`.

```suggestion
  friend void swap(locale&, locale&) _NOEXCEPT;
};

inline void swap(locale& __lhs, locale& __rhs) _NOEXCEPT {
  std::swap(__lhs.__locale_, __rhs.__locale_);
}
```

Maybe it's better to add a regression test case for this.

https://github.com/llvm/llvm-project/pull/209760


More information about the libcxx-commits mailing list