/*! @file Adapts `std::integer_sequence` for use with Hana. @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) */ #ifndef BOOST_HANA_EXT_STD_INTEGER_SEQUENCE_HPP #define BOOST_HANA_EXT_STD_INTEGER_SEQUENCE_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef BOOST_HANA_DOXYGEN_INVOKED namespace std { //! @ingroup group-ext-std //! Adaptation of `std::integer_sequence` for Hana. //! //! //! //! Modeled concepts //! ---------------- //! 1. `Comparable`\n //! Two `std::integer_sequence`s are equal if and only if they have the //! same number of elements, and if corresponding elements compare equal. //! The types of the elements held in both `integer_sequence`s may be //! different, as long as they can be compared. //! @include example/ext/std/integer_sequence/comparable.cpp //! //! 2. `Foldable`\n //! Folding an `integer_sequence` is equivalent to folding a sequence of //! `std::integral_constant`s with the corresponding types. //! @include example/ext/std/integer_sequence/foldable.cpp //! //! 3. `Iterable`\n //! Iterating over an `integer_sequence` is equivalent to iterating over //! a sequence of the corresponding `std::integral_constant`s. //! @include example/ext/std/integer_sequence/iterable.cpp //! //! 4. `Searchable`\n //! Searching through an `integer_sequence` is equivalent to searching //! through the corresponding sequence of `std::integral_constant`s. //! @include example/ext/std/integer_sequence/searchable.cpp template struct integer_sequence { }; } #endif BOOST_HANA_NAMESPACE_BEGIN namespace ext { namespace std { struct integer_sequence_tag; }} template struct tag_of> { using type = ext::std::integer_sequence_tag; }; ////////////////////////////////////////////////////////////////////////// // Comparable ////////////////////////////////////////////////////////////////////////// template <> struct equal_impl { template static constexpr hana::bool_::value> apply(std::integer_sequence const&, std::integer_sequence const&) { return {}; } template static constexpr hana::false_ apply(Xs const&, Ys const&, ...) { return {}; } }; ////////////////////////////////////////////////////////////////////////// // Foldable ////////////////////////////////////////////////////////////////////////// template <> struct unpack_impl { template static constexpr decltype(auto) apply(std::integer_sequence const&, F&& f) { return static_cast(f)(std::integral_constant{}...); } }; ////////////////////////////////////////////////////////////////////////// // Iterable ////////////////////////////////////////////////////////////////////////// template <> struct at_impl { template static constexpr auto apply(std::integer_sequence const&, N const&) { constexpr std::size_t n = N::value; constexpr T values_[] = {v...}; return std::integral_constant{}; } }; template <> struct drop_front_impl { template static constexpr auto drop_front_helper(std::integer_sequence, std::index_sequence) { constexpr T ts[sizeof...(t)+1] = {t...}; // avoid 0-sized array return std::integer_sequence{}; } template static constexpr auto apply(std::integer_sequence ts, N const&) { constexpr std::size_t n = N::value; constexpr std::size_t len = sizeof...(t); return drop_front_helper(ts, std::make_index_sequence<(n < len ? len - n : 0)>{}); } }; template <> struct is_empty_impl { template static constexpr auto apply(std::integer_sequence const&) { return hana::bool_c; } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_EXT_STD_INTEGER_SEQUENCE_HPP