take_back.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*!
  2. @file
  3. Forward declares `boost::hana::take_back`.
  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_FWD_TAKE_BACK_HPP
  9. #define BOOST_HANA_FWD_TAKE_BACK_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. #include <cstddef>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! Returns the last `n` elements of a sequence, or the whole sequence
  15. //! if the sequence has less than `n` elements.
  16. //! @ingroup group-Sequence
  17. //!
  18. //! Given a `Sequence` `xs` and an `IntegralConstant` `n`, `take_back(xs, n)`
  19. //! is a new sequence containing the last `n` elements of `xs`, in the
  20. //! same order. If `length(xs) <= n`, the whole sequence is returned and
  21. //! no error is triggered.
  22. //!
  23. //!
  24. //! @param xs
  25. //! The sequence to take the elements from.
  26. //!
  27. //! @param n
  28. //! A non-negative `IntegralConstant` representing the number of elements
  29. //! to keep in the resulting sequence.
  30. //!
  31. //!
  32. //! Example
  33. //! -------
  34. //! @include example/take_back.cpp
  35. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  36. constexpr auto take_back = [](auto&& xs, auto const& n) {
  37. return tag-dispatched;
  38. };
  39. #else
  40. template <typename S, typename = void>
  41. struct take_back_impl : take_back_impl<S, when<true>> { };
  42. struct take_back_t {
  43. template <typename Xs, typename N>
  44. constexpr auto operator()(Xs&& xs, N const& n) const;
  45. };
  46. constexpr take_back_t take_back{};
  47. #endif
  48. //! Equivalent to `take_back`; provided for convenience.
  49. //! @ingroup group-Sequence
  50. //!
  51. //!
  52. //! Example
  53. //! -------
  54. //! @include example/take_back_c.cpp
  55. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  56. template <std::size_t n>
  57. constexpr auto take_back_c = [](auto&& xs) {
  58. return hana::take_back(forwarded(xs), hana::size_c<n>);
  59. };
  60. #else
  61. template <std::size_t n>
  62. struct take_back_c_t;
  63. template <std::size_t n>
  64. constexpr take_back_c_t<n> take_back_c{};
  65. #endif
  66. BOOST_HANA_NAMESPACE_END
  67. #endif // !BOOST_HANA_FWD_TAKE_BACK_HPP