Boost.Hana  1.5.0
Your standard library for metaprogramming
std::array< T, N > Struct Template Reference

Description

template<typename T, std::size_t N>
struct std::array< T, N >

Adaptation of std::array for Hana.

Modeled concepts

  1. Comparable
    std::arrays are compared as per std::equal, except that two arrays with different sizes compare unequal instead of triggering an error and the result of the comparison is constexpr if both arrays are constexpr.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    #include <array>
    namespace hana = boost::hana;
    constexpr std::array<int, 4> xs = {{1, 2, 3, 4}};
    constexpr std::array<int, 5> ys = {{1, 2, 3, 4, 5}};
    // arrays have different constexpr contents; result is a constexpr bool
    static_assert(hana::equal(xs, xs), "");
    // arrays have different lengths; result is an integral_constant
    int main() { }
  2. Orderable
    std::arrays are ordered with the usual lexicographical ordering, except that two arrays with different size can be ordered instead of triggering an error and the result of the comparison is constexpr if both arrays are constexpr.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    #include <array>
    namespace hana = boost::hana;
    constexpr std::array<int, 4> evens = {{2, 4, 6, 8}};
    constexpr std::array<int, 4> odds = {{1, 3, 5, 7}};
    constexpr std::array<int, 5> up_to_5 = {{1, 2, 3, 4, 5}};
    // arrays with same length
    static_assert(hana::less(odds, evens), "");
    // arrays with different lengths
    static_assert(hana::less(up_to_5, odds), "");
    int main() { }
  3. Foldable
    Folding an array from the left is equivalent to calling std::accumulate on it, except it can be constexpr.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    #include <array>
    namespace hana = boost::hana;
    int main() {
    std::array<int, 5> a = {{0, 1, 2, 3, 4}};
    auto b = hana::unpack(a, [](auto ...i) {
    return std::array<int, sizeof...(i)>{{(i + 10)...}};
    });
    BOOST_HANA_RUNTIME_CHECK(b == std::array<int, 5>{{10, 11, 12, 13, 14}});
    }
  4. Iterable
    Iterating over a std::array is equivalent to iterating over it with a normal for loop.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    #include <array>
    namespace hana = boost::hana;
    constexpr std::array<int, 5> a = {{0, 1, 2, 3, 4}};
    static_assert(hana::at_c<2>(a) == 2, "");
    static_assert(hana::equal(hana::drop_front(a), std::array<int, 4>{{1, 2, 3, 4}}), "");
    int main() { }