[libcxx-commits] [PATCH] D104760: [libcxx][NFC] removes `swap`'s dependency on `swap_ranges`

Christopher Di Bella via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 22 18:56:25 PDT 2021


cjdb created this revision.
cjdb added a reviewer: ldionne.
cjdb requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Under the as-if rule, we can directly implement the array overload for
`std::swap`. By removing this circular dependency where `swap` is
implemented in terms of `swap_ranges` and `swap_ranges` is defined in
terms of `swap`, we can split them into their own headers. This will:

(a) limit the surface area in which Hyrum's law can bite us;
(b) force users to include the correct headers;
(c) make finding the definitions trivial (`swap` is a utility,

  `swap_ranges` is an algorithm).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104760

Files:
  libcxx/include/type_traits
  libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp


Index: libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp
===================================================================
--- libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp
+++ libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp
@@ -13,9 +13,9 @@
 //   void
 //   swap(T (&a)[N], T (&b)[N]);
 
-#include <utility>
 #include <cassert>
 #include <memory>
+#include <utility>
 
 #include "test_macros.h"
 
Index: libcxx/include/type_traits
===================================================================
--- libcxx/include/type_traits
+++ libcxx/include/type_traits
@@ -4221,7 +4221,8 @@
 >::type
 swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
 {
-    _VSTD::swap_ranges(__a, __a + _Np, __b);
+    for (size_t __i = 0; __i != _Np; ++__i)
+        swap(__a[__i], __b[__i]);
 }
 
 template <class _ForwardIterator1, class _ForwardIterator2>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104760.353840.patch
Type: text/x-patch
Size: 935 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210623/5ab0264d/attachment.bin>


More information about the libcxx-commits mailing list