monadic_compose.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*!
  2. @file
  3. Defines `boost::hana::monadic_compose`.
  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_MONADIC_COMPOSE_HPP
  9. #define BOOST_HANA_MONADIC_COMPOSE_HPP
  10. #include <boost/hana/fwd/monadic_compose.hpp>
  11. #include <boost/hana/chain.hpp>
  12. #include <boost/hana/concept/monad.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/functional/partial.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. namespace detail {
  18. struct monadic_compose_helper {
  19. template <typename F, typename G, typename X>
  20. constexpr decltype(auto) operator()(F&& f, G&& g, X&& x) const {
  21. using M = typename hana::tag_of<decltype(g(x))>::type;
  22. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  23. static_assert(hana::Monad<M>::value,
  24. "hana::monadic_compose(f, g) requires 'g' to return a monadic value");
  25. #endif
  26. return hana::chain(static_cast<G&&>(g)(static_cast<X&&>(x)),
  27. static_cast<F&&>(f));
  28. }
  29. };
  30. }
  31. //! @cond
  32. template <typename F, typename G>
  33. constexpr auto monadic_compose_t::operator()(F&& f, G&& g) const {
  34. return hana::partial(detail::monadic_compose_helper{},
  35. static_cast<F&&>(f),
  36. static_cast<G&&>(g)
  37. );
  38. }
  39. //! @endcond
  40. BOOST_HANA_NAMESPACE_END
  41. #endif // !BOOST_HANA_MONADIC_COMPOSE_HPP