eval.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*!
  2. @file
  3. Forward declares `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_FWD_EVAL_HPP
  9. #define BOOST_HANA_FWD_EVAL_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Evaluate a lazy value and return it.
  14. //! @relates hana::lazy
  15. //!
  16. //! Given a lazy expression `expr`, `eval` evaluates `expr` and returns
  17. //! the result as a normal value. However, for convenience, `eval` can
  18. //! also be used with nullary and unary function objects. Specifically,
  19. //! if `expr` is not a `hana::lazy`, it is called with no arguments at
  20. //! all and the result of that call (`expr()`) is returned. Otherwise,
  21. //! if `expr()` is ill-formed, then `expr(hana::id)` is returned instead.
  22. //! If that expression is ill-formed, then a compile-time error is
  23. //! triggered.
  24. //!
  25. //! The reason for allowing nullary callables in `eval` is because this
  26. //! allows using nullary lambdas as lazy branches to `eval_if`, which
  27. //! is convenient. The reason for allowing unary callables and calling
  28. //! them with `hana::id` is because this allows deferring the
  29. //! compile-time evaluation of selected expressions inside the callable.
  30. //! How this can be achieved is documented by `hana::eval_if`.
  31. //!
  32. //!
  33. //! Example
  34. //! -------
  35. //! @include example/eval.cpp
  36. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  37. constexpr auto eval = [](auto&& see_documentation) -> decltype(auto) {
  38. return tag-dispatched;
  39. };
  40. #else
  41. template <typename T, typename = void>
  42. struct eval_impl : eval_impl<T, when<true>> { };
  43. struct eval_t {
  44. template <typename Expr>
  45. constexpr decltype(auto) operator()(Expr&& expr) const;
  46. };
  47. constexpr eval_t eval{};
  48. #endif
  49. BOOST_HANA_NAMESPACE_END
  50. #endif // !BOOST_HANA_FWD_EVAL_HPP