diff --git a/libcxx/include/__algorithm/sort.h b/libcxx/include/__algorithm/sort.h index 1b878c33c7a16..6f6d7bd87a593 100644 --- a/libcxx/include/__algorithm/sort.h +++ b/libcxx/include/__algorithm/sort.h @@ -28,8 +28,8 @@ #include <__iterator/iterator_traits.h> #include <__type_traits/conditional.h> #include <__type_traits/disjunction.h> -#include <__type_traits/is_arithmetic.h> #include <__type_traits/is_constant_evaluated.h> +#include <__type_traits/is_trivially_copyable.h> #include <__utility/move.h> #include <__utility/pair.h> #include @@ -144,7 +144,7 @@ template ::value && sizeof(_Tp) <= sizeof(void*) && - is_arithmetic<_Tp>::value && __is_simple_comparator<_Compare>::value>; + is_trivially_copyable<_Tp>::value && __is_simple_comparator<_Compare>::value>; namespace __detail { @@ -158,6 +158,8 @@ template inline _LIBCPP_HIDE_FROM_ABI void __cond_swap(_RandomAccessIterator __x, _RandomAccessIterator __y, _Compare __c) { // Note: this function behaves correctly even with proxy iterators (because it relies on `value_type`). using value_type = typename iterator_traits<_RandomAccessIterator>::value_type; + static_assert(is_trivially_copyable::value, + "Copying must have no side effects distinguishable from swapping/moving"); bool __r = __c(*__x, *__y); value_type __tmp = __r ? *__x : *__y; *__y = __r ? *__y : *__x; @@ -171,6 +173,8 @@ inline _LIBCPP_HIDE_FROM_ABI void __partially_sorted_swap(_RandomAccessIterator __x, _RandomAccessIterator __y, _RandomAccessIterator __z, _Compare __c) { // Note: this function behaves correctly even with proxy iterators (because it relies on `value_type`). using value_type = typename iterator_traits<_RandomAccessIterator>::value_type; + static_assert(is_trivially_copyable::value, + "Copying must have no side effects distinguishable from swapping/moving"); bool __r = __c(*__z, *__x); value_type __tmp = __r ? *__z : *__x; *__z = __r ? *__x : *__z; diff --git a/libcxx/test/libcxx/algorithms/sort_stability.pass.cpp b/libcxx/test/libcxx/algorithms/sort_stability.pass.cpp index 712f12c255935..8856ca019f721 100644 --- a/libcxx/test/libcxx/algorithms/sort_stability.pass.cpp +++ b/libcxx/test/libcxx/algorithms/sort_stability.pass.cpp @@ -68,8 +68,7 @@ void test_same() { auto snapshot_custom_v = v; std::sort(v.begin(), v.end()); std::sort(snapshot_v.begin(), snapshot_v.end()); - std::sort(snapshot_custom_v.begin(), snapshot_custom_v.end(), - [](const EqualType&, const EqualType&) { return false; }); + std::sort(snapshot_custom_v.begin(), snapshot_custom_v.end(), std::less()); bool all_equal = true; for (int i = 0; i < kSize; ++i) { if (v[i].value != snapshot_v[i].value || v[i].value != snapshot_custom_v[i].value) {