From 3a4cc6281b56c52136cc755e46776c025114d865 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 5 Dec 2019 00:42:06 +0000 Subject: libstdc++: Implement spaceship for std::array (P1614R2) As done for std::pair, this defines operator<=> as a non-member function template and does not alter operator==, as expected to be proposed as the resolution to an unpublished LWG issue. Instead of calling std::lexicographical_compare_three_way the <=> overload is implemented by hand to take advantage of the fact the element types and array sizes are known to be the same. * include/bits/cpp_type_traits.h (__is_byte): Add specialization. * include/std/array (operator<=>): Likewise. * testsuite/23_containers/array/comparison_operators/constexpr.cc: Test three-way comparisons and arrays of unsigned char. * testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust dg-error line numbers. From-SVN: r278981 --- libstdc++-v3/include/std/array | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'libstdc++-v3/include/std/array') diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index 9ad1e65..ad3f651 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -253,6 +253,25 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) { return std::equal(__one.begin(), __one.end(), __two.begin()); } +#if __cpp_lib_three_way_comparison && __cpp_lib_concepts + template + constexpr __detail::__synth3way_t<_Tp> + operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) + { + if constexpr (_Nm && __is_byte<_Tp>::__value) + return __builtin_memcmp(__a.data(), __b.data(), _Nm) <=> 0; + else + { + for (size_t __i = 0; __i < _Nm; ++__i) + { + auto __c = __detail::__synth3way(__a[__i], __b[__i]); + if (__c != 0) + return __c; + } + } + return strong_ordering::equal; + } +#else template _GLIBCXX20_CONSTEXPR inline bool @@ -285,6 +304,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER inline bool operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) { return !(__one < __two); } +#endif // three_way_comparison && concepts // Specialized algorithms. template -- cgit v1.1