[libcxx-commits] [PATCH] D119445: [libc++] Use std::ranges::swap_ranges in swap CPO

Joe Loser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 10 07:57:40 PST 2022


jloser created this revision.
jloser added reviewers: ldionne, Quuxplusone, Mordante, philnik, var-const.
jloser requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

As the comment notes, now that we have implemented `std::ranges::swap_ranges`,
we can apply it when swapping C-style arrays in the swap CPO.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119445

Files:
  libcxx/include/__concepts/swappable.h


Index: libcxx/include/__concepts/swappable.h
===================================================================
--- libcxx/include/__concepts/swappable.h
+++ libcxx/include/__concepts/swappable.h
@@ -9,11 +9,13 @@
 #ifndef _LIBCPP___CONCEPTS_SWAPPABLE_H
 #define _LIBCPP___CONCEPTS_SWAPPABLE_H
 
+#include <__algorithm/swap_ranges.h>
 #include <__concepts/assignable.h>
 #include <__concepts/class_or_enum.h>
 #include <__concepts/common_reference_with.h>
 #include <__concepts/constructible.h>
 #include <__config>
+#include <__memory/addressof.h>
 #include <__utility/exchange.h>
 #include <__utility/forward.h>
 #include <__utility/move.h>
@@ -75,10 +77,7 @@
     constexpr void operator()(_Tp(& __t)[_Size], _Up(& __u)[_Size]) const
       noexcept(noexcept((*this)(*__t, *__u)))
     {
-      // TODO(cjdb): replace with `ranges::swap_ranges`.
-      for (size_t __i = 0; __i < _Size; ++__i) {
-        (*this)(__t[__i], __u[__i]);
-      }
+      _VSTD::swap_ranges(_VSTD::addressof(__t[0]), _VSTD::addressof(__t[_Size]), _VSTD::addressof(__u[0]));
     }
 
     // 2.3   Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119445.407540.patch
Type: text/x-patch
Size: 1155 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220210/1669537f/attachment.bin>


More information about the libcxx-commits mailing list