take.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*!
  2. @file
  3. Defines `boost::hana::detail::variadic::take`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_DETAIL_VARIADIC_TAKE_HPP
  9. #define BOOST_HANA_DETAIL_VARIADIC_TAKE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/detail/variadic/split_at.hpp>
  12. #include <boost/hana/functional/always.hpp>
  13. #include <boost/hana/functional/reverse_partial.hpp>
  14. #include <cstddef>
  15. BOOST_HANA_NAMESPACE_BEGIN namespace detail { namespace variadic {
  16. struct take_impl2 {
  17. template <typename F, typename ...Xs>
  18. constexpr decltype(auto) operator()(F&& f, Xs&& ...xs) const {
  19. return static_cast<F&&>(f)(static_cast<Xs&&>(xs)...);
  20. }
  21. };
  22. struct take_impl1 {
  23. template <typename ...Xs>
  24. constexpr auto operator()(Xs&& ...xs) const {
  25. return hana::always(
  26. reverse_partial(take_impl2{},
  27. static_cast<Xs&&>(xs)...)
  28. );
  29. }
  30. };
  31. template <std::size_t n>
  32. struct take_t {
  33. template <typename ...Xs>
  34. constexpr decltype(auto) operator()(Xs&& ...xs) const {
  35. return variadic::split_at<n>(static_cast<Xs&&>(xs)...)(take_impl1{});
  36. }
  37. };
  38. template <std::size_t n>
  39. constexpr take_t<n> take{};
  40. }} BOOST_HANA_NAMESPACE_END
  41. #endif // !BOOST_HANA_DETAIL_VARIADIC_TAKE_HPP