From 37289c5d92aa4ef9b4203e49746ddd733e984ab1 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 23 Feb 2025 20:45:50 +0200 Subject: [PATCH] Add to_array --- include/boost/array.hpp | 65 ++++++++++++++++++++---- test/Jamfile.v2 | 1 + test/to_array_test.cpp | 107 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 10 deletions(-) create mode 100644 test/to_array_test.cpp diff --git a/include/boost/array.hpp b/include/boost/array.hpp index 601fee8..bea6f98 100644 --- a/include/boost/array.hpp +++ b/include/boost/array.hpp @@ -430,17 +430,62 @@ namespace boost { return arg.elems; } - template - BOOST_CXX14_CONSTEXPR T &get(boost::array &arr) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(boost::array &) index out of range" ); - return arr[Idx]; - } + template + BOOST_CXX14_CONSTEXPR T &get(boost::array &arr) BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(boost::array &) index out of range" ); + return arr[Idx]; + } - template - BOOST_CONSTEXPR const T &get(const boost::array &arr) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(const boost::array &) index out of range" ); - return arr[Idx]; - } + template + BOOST_CONSTEXPR const T &get(const boost::array &arr) BOOST_NOEXCEPT + { + BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(const boost::array &) index out of range" ); + return arr[Idx]; + } + + template + BOOST_CXX14_CONSTEXPR array to_array( T const (&a)[ N ] ) + { + array r = {}; + + for( std::size_t i = 0; i < N; ++i ) + { + r[ i ] = a[ i ]; + } + + return r; + } + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + + template + BOOST_CXX14_CONSTEXPR array to_array( T (&&a)[ N ] ) + { + array r = {}; + + for( std::size_t i = 0; i < N; ++i ) + { + r[ i ] = std::move( a[ i ] ); + } + + return r; + } + + template + BOOST_CXX14_CONSTEXPR array to_array( T const (&&a)[ N ] ) + { + array r = {}; + + for( std::size_t i = 0; i < N; ++i ) + { + r[ i ] = a[ i ]; + } + + return r; + } + +#endif } /* namespace boost */ diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 7243765..02c6464 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -84,6 +84,7 @@ run array_eq_test.cpp ; run array_lt_test.cpp ; run array_thw_test.cpp ; run array_get_test.cpp ; +run to_array_test.cpp ; # C++11 constexpr diff --git a/test/to_array_test.cpp b/test/to_array_test.cpp new file mode 100644 index 0000000..6d4cd83 --- /dev/null +++ b/test/to_array_test.cpp @@ -0,0 +1,107 @@ +// 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 +#include + +boost::array f1() +{ + boost::array r = {{ 1, 2, 3 }}; + return r; +} + +boost::array const f2() +{ + boost::array r = {{ 1, 2, 3 }}; + return r; +} + +int main() +{ + { + int a[] = { 1, 2, 3 }; + + boost::array b = boost::to_array( a ); + + BOOST_TEST_EQ( b[0], 1 ); + BOOST_TEST_EQ( b[1], 2 ); + BOOST_TEST_EQ( b[2], 3 ); + } + + { + int const a[] = { 1, 2, 3 }; + + boost::array b = boost::to_array( a ); + + BOOST_TEST_EQ( b[0], 1 ); + BOOST_TEST_EQ( b[1], 2 ); + BOOST_TEST_EQ( b[2], 3 ); + } + + { + boost::array b = boost::to_array( f1().elems ); + + BOOST_TEST_EQ( b[0], 1 ); + BOOST_TEST_EQ( b[1], 2 ); + BOOST_TEST_EQ( b[2], 3 ); + } + + { + boost::array b = boost::to_array( f2().elems ); + + BOOST_TEST_EQ( b[0], 1 ); + BOOST_TEST_EQ( b[1], 2 ); + BOOST_TEST_EQ( b[2], 3 ); + } + +#if BOOST_CXX_VERSION >= 201103L + +#if !BOOST_WORKAROUND(BOOST_MSVC, < 1910) + { + int a[] = { 1, 2, 3 }; + + boost::array b = boost::to_array( std::move( a ) ); + + BOOST_TEST_EQ( b[0], 1 ); + BOOST_TEST_EQ( b[1], 2 ); + BOOST_TEST_EQ( b[2], 3 ); + } + + { + int const a[] = { 1, 2, 3 }; + + boost::array b = boost::to_array( std::move( a ) ); + + BOOST_TEST_EQ( b[0], 1 ); + BOOST_TEST_EQ( b[1], 2 ); + BOOST_TEST_EQ( b[2], 3 ); + } +#endif + + { + boost::array b = boost::to_array({ 1, 2, 3 }); + + BOOST_TEST_EQ( b[0], 1 ); + BOOST_TEST_EQ( b[1], 2 ); + BOOST_TEST_EQ( b[2], 3 ); + } + +#if !BOOST_WORKAROUND(BOOST_MSVC, < 1920) + { + std::unique_ptr a[] = { {}, {}, {} }; + + boost::array, 3> b = boost::to_array( std::move( a ) ); + + (void)b; + } +#endif + +#endif + + return boost::report_errors(); +}