eval.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*!
  2. @file
  3. Defines `boost::hana::eval`.
  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_EVAL_HPP
  9. #define BOOST_HANA_EVAL_HPP
  10. #include <boost/hana/fwd/eval.hpp>
  11. #include <boost/hana/config.hpp>
  12. #include <boost/hana/core/dispatch.hpp>
  13. #include <boost/hana/detail/wrong.hpp>
  14. #include <boost/hana/functional/id.hpp>
  15. BOOST_HANA_NAMESPACE_BEGIN
  16. //! @cond
  17. template <typename Expr>
  18. constexpr decltype(auto) eval_t::operator()(Expr&& expr) const {
  19. return eval_impl<typename hana::tag_of<Expr>::type>::apply(
  20. static_cast<Expr&&>(expr)
  21. );
  22. }
  23. //! @endcond
  24. template <typename T, bool condition>
  25. struct eval_impl<T, when<condition>> : default_ {
  26. template <typename Expr>
  27. static constexpr auto eval_helper(Expr&& expr, int)
  28. -> decltype(static_cast<Expr&&>(expr)())
  29. { return static_cast<Expr&&>(expr)(); }
  30. template <typename Expr>
  31. static constexpr auto eval_helper(Expr&& expr, long)
  32. -> decltype(static_cast<Expr&&>(expr)(hana::id))
  33. { return static_cast<Expr&&>(expr)(hana::id); }
  34. template <typename Expr>
  35. static constexpr auto eval_helper(Expr&&, ...) {
  36. static_assert(detail::wrong<Expr>{},
  37. "hana::eval(expr) requires the expression to be a hana::lazy, "
  38. "a nullary Callable or a unary Callable that may be "
  39. "called with hana::id");
  40. }
  41. template <typename Expr>
  42. static constexpr decltype(auto) apply(Expr&& expr)
  43. { return eval_helper(static_cast<Expr&&>(expr), int{}); }
  44. };
  45. BOOST_HANA_NAMESPACE_END
  46. #endif // !BOOST_HANA_EVAL_HPP