eval_if.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*!
  2. @file
  3. Forward declares `boost::hana::eval_if`.
  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_IF_HPP
  9. #define BOOST_HANA_FWD_EVAL_IF_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Conditionally execute one of two branches based on a condition.
  14. //! @ingroup group-Logical
  15. //!
  16. //! Given a condition and two branches in the form of lambdas or
  17. //! `hana::lazy`s, `eval_if` will evaluate the branch selected by the
  18. //! condition with `eval` and return the result. The exact requirements
  19. //! for what the branches may be are the same requirements as those for
  20. //! the `eval` function.
  21. //!
  22. //!
  23. //! Deferring compile-time evaluation inside `eval_if`
  24. //! --------------------------------------------------
  25. //! By passing a unary callable to `eval_if`, it is possible to defer
  26. //! the compile-time evaluation of selected expressions inside the
  27. //! lambda. This is useful when instantiating a branch would trigger
  28. //! a compile-time error; we only want the branch to be instantiated
  29. //! when that branch is selected. Here's how it can be achieved.
  30. //!
  31. //! For simplicity, we'll use a unary lambda as our unary callable.
  32. //! Our lambda must accept a parameter (usually called `_`), which
  33. //! can be used to defer the compile-time evaluation of expressions
  34. //! as required. For example,
  35. //! @code
  36. //! template <typename N>
  37. //! auto fact(N n) {
  38. //! return hana::eval_if(n == hana::int_c<0>,
  39. //! [] { return hana::int_c<1>; },
  40. //! [=](auto _) { return n * fact(_(n) - hana::int_c<1>); }
  41. //! );
  42. //! }
  43. //! @endcode
  44. //!
  45. //! What happens here is that `eval_if` will call `eval` on the selected
  46. //! branch. In turn, `eval` will call the selected branch either with
  47. //! nothing -- for the _then_ branch -- or with `hana::id` -- for the
  48. //! _else_ branch. Hence, `_(x)` is always the same as `x`, but the
  49. //! compiler can't tell until the lambda has been called! Hence, the
  50. //! compiler has to wait before it instantiates the body of the lambda
  51. //! and no infinite recursion happens. However, this trick to delay the
  52. //! instantiation of the lambda's body can only be used when the condition
  53. //! is known at compile-time, because otherwise both branches have to be
  54. //! instantiated inside the `eval_if` anyway.
  55. //!
  56. //! There are several caveats to note with this approach to lazy branching.
  57. //! First, because we're using lambdas, it means that the function's
  58. //! result can't be used in a constant expression. This is a limitation
  59. //! of the current language.
  60. //!
  61. //! The second caveat is that compilers currently have several bugs
  62. //! regarding deeply nested lambdas with captures. So you always risk
  63. //! crashing the compiler, but this is a question of time before it is
  64. //! not a problem anymore.
  65. //!
  66. //! Finally, it means that conditionals can't be written directly inside
  67. //! unevaluated contexts. The reason is that a lambda can't appear in an
  68. //! unevaluated context, for example in `decltype`. One way to workaround
  69. //! this is to completely lift your type computations into variable
  70. //! templates instead. For example, instead of writing
  71. //! @code
  72. //! template <typename T>
  73. //! struct pointerize : decltype(
  74. //! hana::eval_if(hana::traits::is_pointer(hana::type_c<T>),
  75. //! [] { return hana::type_c<T>; },
  76. //! [](auto _) { return _(hana::traits::add_pointer)(hana::type_c<T>); }
  77. //! ))
  78. //! { };
  79. //! @endcode
  80. //!
  81. //! you could instead write
  82. //!
  83. //! @code
  84. //! template <typename T>
  85. //! auto pointerize_impl(T t) {
  86. //! return hana::eval_if(hana::traits::is_pointer(t),
  87. //! [] { return hana::type_c<T>; },
  88. //! [](auto _) { return _(hana::traits::add_pointer)(hana::type_c<T>); }
  89. //! );
  90. //! }
  91. //!
  92. //! template <typename T>
  93. //! using pointerize = decltype(pointerize_impl(hana::type_c<T>));
  94. //! @endcode
  95. //!
  96. //! > __Note__: This example would actually be implemented more easily
  97. //! > with partial specializations, but my bag of good examples is empty
  98. //! > at the time of writing this.
  99. //!
  100. //! Now, this hoop-jumping only has to be done in one place, because
  101. //! you should use normal function notation everywhere else in your
  102. //! metaprogram to perform type computations. So the syntactic
  103. //! cost is amortized over the whole program.
  104. //!
  105. //! Another way to work around this limitation of the language would be
  106. //! to use `hana::lazy` for the branches. However, this is only suitable
  107. //! when the branches are not too complicated. With `hana::lazy`, you
  108. //! could write the previous example as
  109. //! @code
  110. //! template <typename T>
  111. //! struct pointerize : decltype(
  112. //! hana::eval_if(hana::traits::is_pointer(hana::type_c<T>),
  113. //! hana::make_lazy(hana::type_c<T>),
  114. //! hana::make_lazy(hana::traits::add_pointer)(hana::type_c<T>)
  115. //! ))
  116. //! { };
  117. //! @endcode
  118. //!
  119. //!
  120. //! @param cond
  121. //! The condition determining which of the two branches is selected.
  122. //!
  123. //! @param then
  124. //! An expression called as `eval(then)` if `cond` is true-valued.
  125. //!
  126. //! @param else_
  127. //! A function called as `eval(else_)` if `cond` is false-valued.
  128. //!
  129. //!
  130. //! Example
  131. //! -------
  132. //! @include example/eval_if.cpp
  133. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  134. constexpr auto eval_if = [](auto&& cond, auto&& then, auto&& else_) -> decltype(auto) {
  135. return tag-dispatched;
  136. };
  137. #else
  138. template <typename L, typename = void>
  139. struct eval_if_impl : eval_if_impl<L, when<true>> { };
  140. struct eval_if_t {
  141. template <typename Cond, typename Then, typename Else>
  142. constexpr decltype(auto) operator()(Cond&& cond, Then&& then, Else&& else_) const;
  143. };
  144. constexpr eval_if_t eval_if{};
  145. #endif
  146. BOOST_HANA_NAMESPACE_END
  147. #endif // !BOOST_HANA_FWD_EVAL_IF_HPP