3 Defines `boost::hana::drop_back`.
5 @copyright Louis Dionne 2013-2016
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_BACK_HPP
11 #define BOOST_HANA_DROP_BACK_HPP
13 #include <boost/hana/fwd/drop_back.hpp>
15 #include <boost/hana/at.hpp>
16 #include <boost/hana/concept/integral_constant.hpp>
17 #include <boost/hana/concept/sequence.hpp>
18 #include <boost/hana/config.hpp>
19 #include <boost/hana/core/dispatch.hpp>
20 #include <boost/hana/core/make.hpp>
21 #include <boost/hana/integral_constant.hpp>
22 #include <boost/hana/length.hpp>
28 BOOST_HANA_NAMESPACE_BEGIN
30 template <typename Xs, typename N>
31 constexpr auto drop_back_t::operator()(Xs&& xs, N const& n) const {
32 using S = typename hana::tag_of<Xs>::type;
33 using DropBack = BOOST_HANA_DISPATCH_IF(drop_back_impl<S>,
34 hana::Sequence<S>::value &&
35 hana::IntegralConstant<N>::value
38 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
39 static_assert(hana::Sequence<S>::value,
40 "hana::drop_back(xs, n) requires 'xs' to be a Sequence");
42 static_assert(hana::IntegralConstant<N>::value,
43 "hana::drop_back(xs, n) requires 'n' to be an IntegralConstant");
46 static_assert(N::value >= 0,
47 "hana::drop_back(xs, n) requires 'n' to be non-negative");
49 return DropBack::apply(static_cast<Xs&&>(xs), n);
52 template <typename Xs>
53 constexpr auto drop_back_t::operator()(Xs&& xs) const {
54 return (*this)(static_cast<Xs&&>(xs), hana::size_c<1>);
58 template <typename S, bool condition>
59 struct drop_back_impl<S, when<condition>> : default_ {
60 template <typename Xs, std::size_t ...n>
61 static constexpr auto drop_back_helper(Xs&& xs, std::index_sequence<n...>) {
62 return hana::make<S>(hana::at_c<n>(static_cast<Xs&&>(xs))...);
65 template <typename Xs, typename N>
66 static constexpr auto apply(Xs&& xs, N const&) {
67 constexpr std::size_t n = N::value;
68 constexpr std::size_t len = decltype(hana::length(xs))::value;
69 return drop_back_helper(static_cast<Xs&&>(xs),
70 std::make_index_sequence<(n > len ? 0 : len - n)>{});
73 BOOST_HANA_NAMESPACE_END
75 #endif // !BOOST_HANA_DROP_BACK_HPP