[libcxx-commits] [libcxx] r365238 - This patch makes swap functions constexpr. Both swap overloads, swap_ranges and iter_swap are updated (with tests).

Zoe Carver via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 5 13:13:34 PDT 2019


Author: zoecarver
Date: Fri Jul  5 13:13:34 2019
New Revision: 365238

URL: http://llvm.org/viewvc/llvm-project?rev=365238&view=rev
Log:
This patch makes swap functions constexpr. Both swap overloads, swap_ranges and iter_swap are updated (with tests).

Modified:
    libcxx/trunk/include/type_traits
    libcxx/trunk/include/utility
    libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp
    libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp
    libcxx/trunk/test/std/utilities/utility/utility.swap/swap.pass.cpp
    libcxx/trunk/test/std/utilities/utility/utility.swap/swap_array.pass.cpp

Modified: libcxx/trunk/include/type_traits
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=365238&r1=365237&r2=365238&view=diff
==============================================================================
--- libcxx/trunk/include/type_traits (original)
+++ libcxx/trunk/include/type_traits Fri Jul  5 13:13:34 2019
@@ -3692,6 +3692,7 @@ typename enable_if
 #else
 void
 #endif
+_LIBCPP_CONSTEXPR_AFTER_CXX17
 swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value &&
                                     is_nothrow_move_assignable<_Tp>::value)
 {
@@ -3701,14 +3702,14 @@ swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_n
 }
 
 template<class _Tp, size_t _Np>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 typename enable_if<
     __is_swappable<_Tp>::value
 >::type
 swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value);
 
 template <class _ForwardIterator1, class _ForwardIterator2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 void
 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
     //                                  _NOEXCEPT_(_NOEXCEPT_(swap(*__a, *__b)))

Modified: libcxx/trunk/include/utility
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/utility?rev=365238&r1=365237&r2=365238&view=diff
==============================================================================
--- libcxx/trunk/include/utility (original)
+++ libcxx/trunk/include/utility Fri Jul  5 13:13:34 2019
@@ -252,7 +252,7 @@ operator>=(const _Tp& __x, const _Tp& __
 
 
 template <class _ForwardIterator1, class _ForwardIterator2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _ForwardIterator2
 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
 {
@@ -263,7 +263,7 @@ swap_ranges(_ForwardIterator1 __first1,
 
 // forward declared in <type_traits>
 template<class _Tp, size_t _Np>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 typename enable_if<
     __is_swappable<_Tp>::value
 >::type

Modified: libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp?rev=365238&r1=365237&r2=365238&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp Fri Jul  5 13:13:34 2019
@@ -18,6 +18,16 @@
 
 #include "test_macros.h"
 
+#if TEST_STD_VER > 17
+constexpr bool test_swap_constexpr()
+{
+    int i = 1;
+    int j = 2;
+    std::iter_swap(&i, &j);
+    return i == 2 && j == 1;
+}
+#endif // TEST_STD_VER > 17
+
 int main(int, char**)
 {
     int i = 1;
@@ -26,5 +36,9 @@ int main(int, char**)
     assert(i == 2);
     assert(j == 1);
 
+#if TEST_STD_VER > 17
+    static_assert(test_swap_constexpr());
+#endif // TEST_STD_VER > 17
+
   return 0;
 }

Modified: libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp?rev=365238&r1=365237&r2=365238&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp Fri Jul  5 13:13:34 2019
@@ -105,6 +105,21 @@ void test2()
     }
 }
 
+#if TEST_STD_VER > 17
+constexpr bool test_swap_constexpr()
+{
+    int i[3] = {1, 2, 3};
+    int j[3] = {4, 5, 6};
+    std::swap_ranges(i, i+3, j);
+    return i[0] == 4 &&
+           i[1] == 5 &&
+           i[2] == 6 &&
+           j[0] == 1 &&
+           j[1] == 2 &&
+           j[2] == 3;
+}
+#endif // TEST_STD_VER > 17
+
 int main(int, char**)
 {
     test<forward_iterator<int*>, forward_iterator<int*> >();
@@ -149,6 +164,10 @@ int main(int, char**)
     test1<std::unique_ptr<int>*, std::unique_ptr<int>*>();
 #endif // TEST_STD_VER >= 11
 
+#if TEST_STD_VER > 17
+    static_assert(test_swap_constexpr());
+#endif // TEST_STD_VER > 17
+
     test2();
 
   return 0;

Modified: libcxx/trunk/test/std/utilities/utility/utility.swap/swap.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/utility/utility.swap/swap.pass.cpp?rev=365238&r1=365237&r2=365238&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/utility/utility.swap/swap.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/utility/utility.swap/swap.pass.cpp Fri Jul  5 13:13:34 2019
@@ -62,6 +62,16 @@ constexpr bool can_swap() {
 }
 #endif
 
+#if TEST_STD_VER > 17
+constexpr bool test_swap_constexpr()
+{
+    int i = 1;
+    int j = 2;
+    std::swap(i, j);
+    return i == 2 && j == 1;
+}
+#endif // TEST_STD_VER > 17
+
 int main(int, char**)
 {
 
@@ -100,5 +110,9 @@ int main(int, char**)
     }
 #endif
 
+#if TEST_STD_VER > 17
+    static_assert(test_swap_constexpr());
+#endif // TEST_STD_VER > 17
+
   return 0;
 }

Modified: libcxx/trunk/test/std/utilities/utility/utility.swap/swap_array.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/utility/utility.swap/swap_array.pass.cpp?rev=365238&r1=365237&r2=365238&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/utility/utility.swap/swap_array.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/utility/utility.swap/swap_array.pass.cpp Fri Jul  5 13:13:34 2019
@@ -53,6 +53,20 @@ constexpr bool can_swap() {
 }
 #endif
 
+#if TEST_STD_VER > 17
+constexpr bool test_swap_constexpr()
+{
+    int i[3] = {1, 2, 3};
+    int j[3] = {4, 5, 6};
+    std::swap(i, j);
+    return i[0] == 4 &&
+           i[1] == 5 &&
+           i[2] == 6 &&
+           j[0] == 1 &&
+           j[1] == 2 &&
+           j[2] == 3;
+}
+#endif // TEST_STD_VER > 17
 
 int main(int, char**)
 {
@@ -98,5 +112,9 @@ int main(int, char**)
     }
 #endif
 
+#if TEST_STD_VER > 17
+    static_assert(test_swap_constexpr());
+#endif // TEST_STD_VER > 17
+
   return 0;
 }




More information about the libcxx-commits mailing list