length.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*!
  2. @file
  3. Defines `boost::hana::length`.
  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_LENGTH_HPP
  9. #define BOOST_HANA_LENGTH_HPP
  10. #include <boost/hana/fwd/length.hpp>
  11. #include <boost/hana/concept/foldable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/integral_constant.hpp>
  15. #include <boost/hana/unpack.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename Xs>
  19. constexpr auto length_t::operator()(Xs const& xs) const {
  20. using S = typename hana::tag_of<Xs>::type;
  21. using Length = BOOST_HANA_DISPATCH_IF(length_impl<S>,
  22. hana::Foldable<S>::value
  23. );
  24. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  25. static_assert(hana::Foldable<S>::value,
  26. "hana::length(xs) requires 'xs' to be Foldable");
  27. #endif
  28. return Length::apply(xs);
  29. }
  30. //! @endcond
  31. namespace detail {
  32. struct argn {
  33. template <typename ...Xs>
  34. constexpr hana::size_t<sizeof...(Xs)> operator()(Xs const& ...) const
  35. { return {}; }
  36. };
  37. }
  38. template <typename T, bool condition>
  39. struct length_impl<T, when<condition>> : default_ {
  40. template <typename Xs>
  41. static constexpr auto apply(Xs const& xs) {
  42. return hana::unpack(xs, detail::argn{});
  43. }
  44. };
  45. BOOST_HANA_NAMESPACE_END
  46. #endif // !BOOST_HANA_LENGTH_HPP