Compare commits

...

15 Commits

22 changed files with 317 additions and 29 deletions

View File

@ -18,3 +18,6 @@ http://www.boost.org/LICENSE_1_0.txt
* Removed obsolete compiler workarounds.
* Changed `array<T, 0>::begin()`, `cbegin()`, `end()`, `cend()` to return `nullptr`, enabling `constexpr`.
This matches the behavior of `std::array`.
* Removed local `hash_value` overload; `boost::hash` supports array-like types natively.
* `array<T, 0>` can now be initialized with `= {{}}`.
* Added `operator\<\=>`.

View File

@ -35,6 +35,9 @@ namespace boost {
template<typename T, std::size_t N>
constexpr bool operator>=(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
constexpr auto operator<=>(const array<T, N>&, const array<T, N>&);
template<std::size_t Idx, typename T, std::size_t N>
constexpr T& get(array<T, N>&) noexcept;
template<std::size_t Idx, typename T, std::size_t N>
@ -136,7 +139,7 @@ public:
template<typename U> array& operator=(const array<U, N>& other);
```
[horizontal]
Effects: :: `std::copy(rhs.begin(), rhs.end(), begin())`.
Effects: :: For each `i` in `[0..N)`, performs `elems[i] = other.elems[i];`.
---
@ -233,7 +236,7 @@ constexpr const_reference operator[](size_type i) const;
[horizontal]
Requires: :: `i < N`.
Returns: :: `elems[i]`.
Throws: :: nothing.
Throws: :: Nothing.
---
@ -254,7 +257,7 @@ constexpr const_reference front() const;
[horizontal]
Requires: :: `N > 0`.
Returns: :: `elems[0]`.
Throws: :: nothing.
Throws: :: Nothing.
---
@ -265,7 +268,7 @@ constexpr const_reference back() const;
[horizontal]
Requires: :: `N > 0`.
Returns: :: `elems[N-1]`.
Throws: :: nothing.
Throws: :: Nothing.
---
@ -302,7 +305,7 @@ Complexity: :: linear in `N`.
void fill(const T& value);
```
[horizontal]
Effects: :: for each `i` in `[0..N)`, performs `elems[i] = value;`.
Effects: :: For each `i` in `[0..N)`, performs `elems[i] = value;`.
---
@ -382,6 +385,17 @@ Returns: :: `!(x < y)`.
---
```
template<typename T, std::size_t N>
constexpr auto operator<=>(const array<T, N>& x, const array<T, N>& y)
-> decltype(x[0] <=> y[0]);
```
[horizontal]
Effects: :: For each `i` in `[0..N)`, if `(x[i] \<\=> y[i]) != 0`, returns `x[i] \<\=> y[i]`. Otherwise, returns `std::strong_ordering::equal`, converted to the return type.
Remarks: :: When `N` is 0, the return type is `std::strong_ordering` and the return value is `std::strong_ordering::equal`.
---
### Specializations
```

View File

@ -37,9 +37,10 @@
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
# pragma warning(push)
# pragma warning(disable:4510) // boost::array<T,N>' : default constructor could not be generated
# pragma warning(disable:4512) // boost::array<T,N>' : assignment operator could not be generated
# pragma warning(disable:4610) // class 'boost::array<T,N>' can never be instantiated - user defined constructor required
# pragma warning(disable: 4510) // boost::array<T,N>' : default constructor could not be generated
# pragma warning(disable: 4512) // boost::array<T,N>' : assignment operator could not be generated
# pragma warning(disable: 4610) // class 'boost::array<T,N>' can never be instantiated - user defined constructor required
# pragma warning(disable: 4702) // unreachable code
#endif
#include <boost/assert.hpp>
@ -195,6 +196,8 @@ namespace boost {
template< class T >
class array< T, 0 > {
public:
struct {} elems; // enables initialization with = {{}}
public:
// type definitions
@ -324,7 +327,7 @@ namespace boost {
#if BOOST_WORKAROUND(BOOST_GCC, < 90000)
template<class T>
BOOST_CXX14_CONSTEXPR bool operator== (const array<T, 0>& x, const array<T, 0>& y)
BOOST_CXX14_CONSTEXPR bool operator== (const array<T, 0>& /*x*/, const array<T, 0>& /*y*/)
{
return true;
}
@ -351,7 +354,7 @@ namespace boost {
#if BOOST_WORKAROUND(BOOST_GCC, < 90000)
template<class T>
BOOST_CXX14_CONSTEXPR bool operator< (const array<T, 0>& x, const array<T, 0>& y)
BOOST_CXX14_CONSTEXPR bool operator< (const array<T, 0>& /*x*/, const array<T, 0>& /*y*/)
{
return false;
}
@ -379,6 +382,30 @@ namespace boost {
x.swap(y);
}
#if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L
template<class T, std::size_t N>
constexpr auto operator<=> (const array<T,N>& x, const array<T,N>& 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
}
template<class T>
constexpr auto operator<=> (const array<T,0>& /*x*/, const array<T,0>& /*y*/)
-> decltype( 0 <=> 0 )
{
return 0 <=> 0; // std::strong_ordering::equal
}
#endif
// undocumented and obsolete
template <typename T, std::size_t N>
BOOST_DEPRECATED( "please use `elems` instead" )

View File

@ -7,6 +7,38 @@ import testing ;
import-search /boost/config/checks ;
import config : requires ;
project
: requirements
<warnings>extra
<toolset>msvc:<warnings-as-errors>on
<toolset>clang:<warnings-as-errors>on
<toolset>gcc:<warnings-as-errors>on
<toolset>gcc-4.6:<cxxflags>-Wno-missing-braces
<toolset>gcc-4.7:<cxxflags>-Wno-missing-braces
<toolset>gcc-4.6:<cxxflags>-Wno-missing-field-initializers
<toolset>gcc-4.7:<cxxflags>-Wno-missing-field-initializers
<toolset>gcc-4.8:<cxxflags>-Wno-missing-field-initializers
<toolset>gcc-4.9:<cxxflags>-Wno-missing-field-initializers
<toolset>gcc-4.6:<cxxflags>-Wno-type-limits
<toolset>gcc-4.7:<cxxflags>-Wno-type-limits
<toolset>gcc-10:<cxxflags>-Wno-type-limits
<toolset>clang:<cxxflags>-Wno-unnamed-type-template-args
<toolset>clang-3.5:<cxxflags>-Wno-missing-braces
<toolset>clang-3.6:<cxxflags>-Wno-missing-braces
<toolset>clang-3.7:<cxxflags>-Wno-missing-braces
<toolset>clang-3.8:<cxxflags>-Wno-missing-braces
<toolset>clang-3.9:<cxxflags>-Wno-missing-braces
<toolset>clang-4:<cxxflags>-Wno-missing-braces
<toolset>clang-5:<cxxflags>-Wno-missing-braces
;
#
run array0.cpp ;
@ -20,8 +52,10 @@ run array7.cpp ;
compile array_constexpr.cpp ;
compile-fail array_getfail1.cpp ;
compile-fail array_getfail2.cpp ;
compile-fail array_getfail1.cpp
: <warnings>off ;
compile-fail array_getfail2.cpp
: <warnings>off ;
run array_hash.cpp
: : : [ requires cxx11_noexcept ] ;
@ -45,6 +79,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
@ -64,6 +99,7 @@ compile array_access_test_cx2.cpp ;
compile array_fill_test_cx.cpp ;
compile array_eq_test_cx.cpp ;
compile array_lt_test_cx.cpp ;
compile array_thw_test_cx.cpp ;
#

View File

@ -5,6 +5,8 @@
* http://www.boost.org/LICENSE_1_0.txt)
*/
#define BOOST_ALLOW_DEPRECATED_SYMBOLS // assign
#include <iostream>
#include <boost/array.hpp>

View File

@ -5,12 +5,20 @@
* http://www.boost.org/LICENSE_1_0.txt)
*/
#define BOOST_ALLOW_DEPRECATED_SYMBOLS // get_c_array
#include <boost/array.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <string>
#include <iostream>
#include <boost/array.hpp>
#include <algorithm>
#include <boost/core/lightweight_test_trait.hpp>
#if defined(BOOST_GCC) && BOOST_GCC / 10000 == 13
// false -Warray-bounds positive when using -fsanitize=undefined
// restricted to GCC 13 because that's what is tested on Drone
# pragma GCC diagnostic ignored "-Warray-bounds"
#endif
namespace {
template< class T >

View File

@ -5,6 +5,8 @@
* http://www.boost.org/LICENSE_1_0.txt)
*/
#define BOOST_ALLOW_DEPRECATED_SYMBOLS // std::get
#include <string>
#include <iostream>
#include <boost/array.hpp>

View File

@ -2,12 +2,17 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt)
#define BOOST_ALLOW_DEPRECATED_SYMBOLS
// assign is a deprecated nonstandard equivalent of fill
#include <boost/array.hpp>
#include <boost/core/lightweight_test.hpp>
#include <cstddef>
// assign is a nonstandard equivalent of fill
// it probably needs to be deprecated and removed
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
#endif
template<class T, std::size_t N> void test()
{

View File

@ -2,14 +2,15 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt)
#define BOOST_ALLOW_DEPRECATED_SYMBOLS
#include <boost/array.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <cstddef>
// c_array and get_c_array are nonstandard extensions
// probably need to be deprecated and removed
// c_array and get_c_array are deprecated nonstandard extensions
template<class T, std::size_t N> void test()
{

View File

@ -44,11 +44,11 @@ template<class T> void test4()
int main()
{
// test1<int, 0>();
test1<int, 0>();
test1<int, 1>();
test1<int, 7>();
// test1<int const, 0>();
test1<int const, 0>();
test1<int const, 1>();
test1<int const, 7>();

View File

@ -6,6 +6,10 @@
#include <boost/core/lightweight_test.hpp>
#include <cstddef>
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
#endif
template<class T, std::size_t N> void test()
{
{

View File

@ -6,6 +6,10 @@
#include <boost/core/lightweight_test.hpp>
#include <cstddef>
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
#endif
template<class T, std::size_t N> void test()
{
boost::array<T, N> a = {};

View File

@ -8,6 +8,10 @@
#include <boost/config/workaround.hpp>
#include <cstddef>
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
#endif
template<class T, std::size_t N> void test1()
{
boost::array<T, N> a = {{}};
@ -46,11 +50,11 @@ template<class T> void test4()
int main()
{
// test1<int, 0>();
test1<int, 0>();
test1<int, 1>();
test1<int, 7>();
// test1<int const, 0>();
test1<int const, 0>();
test1<int const, 1>();
test1<int const, 7>();

View File

@ -56,7 +56,8 @@ template<class T> void test4()
template<class T> void test5()
{
// constexpr boost::array<T, 0> a = {{}};
constexpr boost::array<T, 0> a = {{}};
(void)a;
}
template<class T> void test6()

View File

@ -94,11 +94,11 @@ template<class T> void test2()
int main()
{
// test<int, 0>();
test<int, 0>();
test<int, 1>();
test<int, 7>();
// test<int const, 0>();
test<int const, 0>();
test<int const, 1>();
test<int const, 7>();

View File

@ -6,6 +6,10 @@
#include <boost/core/lightweight_test.hpp>
#include <cstddef>
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
#endif
template<class T, std::size_t N> void test()
{
{

View File

@ -76,11 +76,11 @@ template<class T, std::size_t N> void test()
int main()
{
// test<int, 0>();
test<int, 0>();
test<int, 1>();
test<int, 7>();
// test<int const, 0>();
test<int const, 0>();
test<int const, 1>();
test<int const, 7>();

View File

@ -15,6 +15,8 @@ template<class T, std::size_t N> void test1()
BOOST_TEST_EQ( a.size(), N );
BOOST_TEST_EQ( a.empty(), N == 0 );
BOOST_TEST_EQ( a.max_size(), N );
(void)a; // msvc-12.0
}
{
@ -23,6 +25,8 @@ template<class T, std::size_t N> void test1()
BOOST_TEST_EQ( a.size(), N );
BOOST_TEST_EQ( a.empty(), N == 0 );
BOOST_TEST_EQ( a.max_size(), N );
(void)a; // msvc-12.0
}
}
@ -49,11 +53,11 @@ template<class T, std::size_t N> void test3()
int main()
{
// test1<int, 0>();
test1<int, 0>();
test1<int, 1>();
test1<int, 7>();
// test1<int const, 0>();
test1<int const, 0>();
test1<int const, 1>();
test1<int const, 7>();

View File

@ -6,6 +6,10 @@
#include <boost/core/lightweight_test.hpp>
#include <cstddef>
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
#endif
template<class T, std::size_t N> void test()
{
boost::array<T, N> a1 = {};

View File

@ -6,6 +6,10 @@
#include <boost/core/lightweight_test.hpp>
#include <cstddef>
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
#endif
template<class T, std::size_t N> void test()
{
boost::array<T, N> a1 = {};

91
test/array_thw_test.cpp Normal file
View File

@ -0,0 +1,91 @@
// Copyright 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt)
#include <boost/array.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#include <cstddef>
#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<class T, std::size_t N> void test()
{
constexpr auto eq = 0 <=> 0;
constexpr auto lt = 0 <=> 1;
constexpr auto gt = 1 <=> 0;
{
boost::array<T, N> const a1 = {};
boost::array<T, N> const a2 = {};
BOOST_TEST( ( a1 <=> a2 ) == eq );
}
{
boost::array<T, N> a1;
boost::array<T, N> a2;
a1.fill( 1 );
a2.fill( 1 );
BOOST_TEST( ( a1 <=> a2 ) == eq );
}
for( std::size_t i = 0; i < N; ++i )
{
boost::array<T, N> a1;
boost::array<T, N> a2;
a1.fill( 1 );
a2.fill( 1 );
a1[ i ] = 0;
BOOST_TEST( ( a1 <=> a2 ) == lt );
{
boost::array<T, N> const a3 = a1;
boost::array<T, N> const a4 = a2;
BOOST_TEST( ( a3 <=> a4 ) == lt );
}
a1[ i ] = 2;
BOOST_TEST( ( a1 <=> a2 ) == gt );
{
boost::array<T, N> const a3 = a1;
boost::array<T, N> const a4 = a2;
BOOST_TEST( ( a3 <=> a4 ) == gt );
}
}
}
int main()
{
test<int, 0>();
test<int, 1>();
test<int, 7>();
test<float, 0>();
test<float, 1>();
test<float, 7>();
return boost::report_errors();
}
#endif

View File

@ -0,0 +1,70 @@
// Copyright 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt)
#include <boost/array.hpp>
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#include <boost/config/workaround.hpp>
#include <cstddef>
#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
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class T, std::size_t N> void test1()
{
constexpr auto eq = 0 <=> 0;
constexpr boost::array<T, N> a1 = {};
constexpr boost::array<T, N> a2 = {};
STATIC_ASSERT( ( a1 <=> a2 ) == eq );
}
template<class T> void test2()
{
constexpr auto eq = 0 <=> 0;
constexpr auto lt = 0 <=> 1;
constexpr auto gt = 1 <=> 0;
{
constexpr boost::array<T, 4> a1 = {{ 1, 2, 3, 4 }};
constexpr boost::array<T, 4> a2 = {{ 1, 2, 3, 4 }};
STATIC_ASSERT( ( a1 <=> a2 ) == eq );
}
{
constexpr boost::array<T, 4> a1 = {{ 1, 2, 3, 4 }};
constexpr boost::array<T, 4> a2 = {{ 1, 2, 3, 5 }};
STATIC_ASSERT( ( a1 <=> a2 ) == lt );
}
{
constexpr boost::array<T, 4> a1 = {{ 1, 2, 3, 4 }};
constexpr boost::array<T, 4> a2 = {{ 1, 2, 3, 2 }};
STATIC_ASSERT( ( a1 <=> a2 ) == gt );
}
}
int main()
{
test1<int, 0>();
test1<int, 1>();
test1<int, 7>();
test2<int>();
}
#endif