diff --git a/include/boost/array.hpp b/include/boost/array.hpp index a66de58..995a888 100644 --- a/include/boost/array.hpp +++ b/include/boost/array.hpp @@ -381,6 +381,23 @@ namespace boost { x.swap(y); } +#if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L + + template + constexpr auto operator<=> (const array& x, const array& y) + -> decltype( x.elems[ 0 ] <=> y.elems[ 0 ] ) + { + for( std::size_t i = 0; i < N; ++i ) + { + auto r = x.elems[ i ] <=> y.elems[ i ]; + if( r != 0 ) return r; + } + + return 0 <=> 0; // std::strong_ordering::equal + } + +#endif + // undocumented and obsolete template BOOST_DEPRECATED( "please use `elems` instead" ) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 2e0ecb1..d88ec6b 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -45,6 +45,7 @@ run array_swap_test.cpp ; run array_swap_test2.cpp ; run array_eq_test.cpp ; run array_lt_test.cpp ; +run array_thw_test.cpp ; run array_get_test.cpp ; # C++11 constexpr diff --git a/test/array_thw_test.cpp b/test/array_thw_test.cpp new file mode 100644 index 0000000..fee7ec8 --- /dev/null +++ b/test/array_thw_test.cpp @@ -0,0 +1,87 @@ +// Copyright 2025 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include + +#if !defined(__cpp_impl_three_way_comparison) + +BOOST_PRAGMA_MESSAGE( "Test skipped because __cpp_impl_three_way_comparison is not defined" ) +int main() {} + +#elif !( __cpp_impl_three_way_comparison >= 201907L ) + +BOOST_PRAGMA_MESSAGE( "Test skipped because __cpp_impl_three_way_comparison is defined to " BOOST_STRINGIZE(__cpp_impl_three_way_comparison) ) +int main() {} + +#else + +template void test() +{ + constexpr auto eq = 0 <=> 0; + constexpr auto lt = 0 <=> 1; + constexpr auto gt = 1 <=> 0; + + { + boost::array const a1 = {}; + boost::array const a2 = {}; + + BOOST_TEST( ( a1 <=> a2 ) == eq ); + } + + { + boost::array a1; + boost::array a2; + + a1.fill( 1 ); + a2.fill( 1 ); + + BOOST_TEST( ( a1 <=> a2 ) == eq ); + } + + for( std::size_t i = 0; i < N; ++i ) + { + boost::array a1; + boost::array a2; + + a1.fill( 1 ); + a2.fill( 1 ); + + a1[ i ] = 0; + + BOOST_TEST( ( a1 <=> a2 ) == lt ); + + { + boost::array const a3 = a1; + boost::array const a4 = a2; + + BOOST_TEST( ( a3 <=> a4 ) == lt ); + } + + a1[ i ] = 2; + + BOOST_TEST( ( a1 <=> a2 ) == gt ); + + { + boost::array const a3 = a1; + boost::array const a4 = a2; + + BOOST_TEST( ( a3 <=> a4 ) == gt ); + } + } +} + +int main() +{ + // test(); + test(); + test(); + + return boost::report_errors(); +} + +#endif