Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:24:45

0001 #ifndef BOOST_COMPAT_TO_ARRAY_HPP_INCLUDED
0002 #define BOOST_COMPAT_TO_ARRAY_HPP_INCLUDED
0003 
0004 // Copyright 2024 Ruben Perez Hidalgo
0005 // Distributed under the Boost Software License, Version 1.0.
0006 // https://www.boost.org/LICENSE_1_0.txt
0007 
0008 #include <boost/compat/integer_sequence.hpp>
0009 #include <boost/compat/type_traits.hpp>
0010 
0011 #include <array>
0012 #include <cstddef>
0013 #include <type_traits>
0014 #include <utility>
0015 
0016 namespace boost {
0017 namespace compat {
0018 
0019 namespace detail {
0020 
0021 template <class T, std::size_t N, std::size_t... I>
0022 constexpr std::array<remove_cv_t<T>, N> to_array_lvalue(T (&a)[N], index_sequence<I...>)
0023 {
0024     return {{a[I]...}};
0025 }
0026 
0027 template <class T, std::size_t N, std::size_t... I>
0028 constexpr std::array<remove_cv_t<T>, N> to_array_rvalue(T (&&a)[N], index_sequence<I...>)
0029 {
0030     return {{std::move(a[I])...}};
0031 }
0032 
0033 }  // namespace detail
0034 
0035 template <class T, std::size_t N>
0036 constexpr std::array<remove_cv_t<T>, N> to_array(T (&a)[N])
0037 {
0038     static_assert(
0039         std::is_constructible<remove_cv_t<T>, T&>::value,
0040         "This overload requires the resulting element type to be constructible from T&"
0041     );
0042     static_assert(!std::is_array<T>::value, "to_array does not work for multi-dimensional C arrays");
0043     return detail::to_array_lvalue(a, make_index_sequence<N>{});
0044 }
0045 
0046 template <class T, std::size_t N>
0047 constexpr std::array<remove_cv_t<T>, N> to_array(T (&&a)[N])
0048 {
0049     static_assert(
0050         std::is_constructible<remove_cv_t<T>, T&&>::value,
0051         "This overload requires the resulting element type to be constructible from T&&"
0052     );
0053     static_assert(!std::is_array<T>::value, "to_array does not work for multi-dimensional C arrays");
0054     return detail::to_array_rvalue(static_cast<T(&&)[N]>(a), make_index_sequence<N>{});
0055 }
0056 
0057 }  // namespace compat
0058 }  // namespace boost
0059 
0060 #endif  // BOOST_COMPAT_TO_ARRAY_HPP_INCLUDED