back.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*!
  2. @file
  3. Defines `boost::hana::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_BACK_HPP
  9. #define BOOST_HANA_BACK_HPP
  10. #include <boost/hana/fwd/back.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/concept/iterable.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/length.hpp>
  16. #include <cstddef>
  17. BOOST_HANA_NAMESPACE_BEGIN
  18. //! @cond
  19. template <typename Xs>
  20. constexpr decltype(auto) back_t::operator()(Xs&& xs) const {
  21. using It = typename hana::tag_of<Xs>::type;
  22. using Back = BOOST_HANA_DISPATCH_IF(back_impl<It>,
  23. hana::Iterable<It>::value
  24. );
  25. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  26. static_assert(hana::Iterable<It>::value,
  27. "hana::back(xs) requires 'xs' to be an Iterable");
  28. #endif
  29. return Back::apply(static_cast<Xs&&>(xs));
  30. }
  31. //! @endcond
  32. template <typename It, bool condition>
  33. struct back_impl<It, when<condition>> : default_ {
  34. template <typename Xs>
  35. static constexpr decltype(auto) apply(Xs&& xs) {
  36. constexpr std::size_t len = decltype(hana::length(xs))::value;
  37. static_assert(len > 0, "hana::back(xs) requires 'xs' to be non-empty");
  38. return hana::at_c<len - 1>(static_cast<Xs&&>(xs));
  39. }
  40. };
  41. BOOST_HANA_NAMESPACE_END
  42. #endif // !BOOST_HANA_BACK_HPP