3 Defines `boost::hana::drop_front_exactly`.
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_DROP_FRONT_EXACTLY_HPP
11 #define BOOST_HANA_DROP_FRONT_EXACTLY_HPP
13 #include <boost/hana/fwd/drop_front_exactly.hpp>
15 #include <boost/hana/bool.hpp>
16 #include <boost/hana/concept/integral_constant.hpp>
17 #include <boost/hana/concept/iterable.hpp>
18 #include <boost/hana/config.hpp>
19 #include <boost/hana/core/dispatch.hpp>
20 #include <boost/hana/drop_front.hpp>
21 #include <boost/hana/integral_constant.hpp>
22 #include <boost/hana/is_empty.hpp>
25 BOOST_HANA_NAMESPACE_BEGIN
27 template <typename Xs, typename N>
28 constexpr auto drop_front_exactly_t::operator()(Xs&& xs, N const& n) const {
29 using It = typename hana::tag_of<Xs>::type;
30 using DropFrontExactly = BOOST_HANA_DISPATCH_IF(drop_front_exactly_impl<It>,
31 hana::Iterable<It>::value &&
32 hana::IntegralConstant<N>::value
35 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
36 static_assert(hana::Iterable<It>::value,
37 "hana::drop_front_exactly(xs, n) requires 'xs' to be an Iterable");
39 static_assert(hana::IntegralConstant<N>::value,
40 "hana::drop_front_exactly(xs, n) requires 'n' to be an IntegralConstant");
43 static_assert(N::value >= 0,
44 "hana::drop_front_exactly(xs, n) requires 'n' to be non-negative");
46 return DropFrontExactly::apply(static_cast<Xs&&>(xs), n);
49 template <typename Xs>
50 constexpr auto drop_front_exactly_t::operator()(Xs&& xs) const {
51 return (*this)(static_cast<Xs&&>(xs), hana::size_c<1>);
56 template <typename Xs, typename N>
57 constexpr void check_dfe_overflow(Xs const& xs, N const&, hana::true_) {
58 constexpr bool n_overflew_length = decltype(
59 hana::is_empty(hana::drop_front(xs, hana::size_c<N::value - 1>))
61 static_assert(!n_overflew_length,
62 "hana::drop_front_exactly(xs, n) requires 'n' to be less than or "
63 "equal to the number of elements in 'xs'");
66 template <typename Xs, typename N>
67 constexpr void check_dfe_overflow(Xs const&, N const&, hana::false_) { }
70 template <typename It, bool condition>
71 struct drop_front_exactly_impl<It, when<condition>> : default_ {
72 template <typename Xs, typename N>
73 static constexpr auto apply(Xs&& xs, N const& n) {
74 auto result = hana::drop_front(static_cast<Xs&&>(xs), n);
75 constexpr bool check_for_overflow =
76 decltype(hana::is_empty(result))::value && N::value != 0;
78 detail::check_dfe_overflow(xs, n, hana::bool_c<check_for_overflow>);
80 return result; // NRVO applied
83 BOOST_HANA_NAMESPACE_END
85 #endif // !BOOST_HANA_DROP_FRONT_EXACTLY_HPP