while.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*!
  2. @file
  3. Forward declares `boost::hana::while_`.
  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_WHILE_HPP
  9. #define BOOST_HANA_FWD_WHILE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Apply a function to an initial state while some predicate is satisfied.
  14. //! @ingroup group-Logical
  15. //!
  16. //! This method is a natural extension of the `while` language construct
  17. //! to manipulate a state whose type may change from one iteration to
  18. //! another. However, note that having a state whose type changes from
  19. //! one iteration to the other is only possible as long as the predicate
  20. //! returns a `Logical` whose truth value is known at compile-time.
  21. //!
  22. //! Specifically, `while_(pred, state, f)` is equivalent to
  23. //! @code
  24. //! f(...f(f(state)))
  25. //! @endcode
  26. //! where `f` is iterated as long as `pred(f(...))` is a true-valued
  27. //! `Logical`.
  28. //!
  29. //!
  30. //! @param pred
  31. //! A predicate called on the state or on the result of applying `f` a
  32. //! certain number of times to the state, and returning whether `f`
  33. //! should be applied one more time.
  34. //!
  35. //! @param state
  36. //! The initial state on which `f` is applied.
  37. //!
  38. //! @param f
  39. //! A function that is iterated on the initial state. Note that the
  40. //! return type of `f` may change from one iteration to the other,
  41. //! but only while `pred` returns a compile-time `Logical`. In other
  42. //! words, `decltype(f(stateN))` may differ from `decltype(f(stateN+1))`,
  43. //! but only if `pred(f(stateN))` returns a compile-time `Logical`.
  44. //!
  45. //!
  46. //! Example
  47. //! -------
  48. //! @include example/while.cpp
  49. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  50. constexpr auto while_ = [](auto&& pred, auto&& state, auto&& f) -> decltype(auto) {
  51. return tag-dispatched;
  52. };
  53. #else
  54. template <typename L, typename = void>
  55. struct while_impl : while_impl<L, when<true>> { };
  56. struct while_t {
  57. template <typename Pred, typename State, typename F>
  58. constexpr decltype(auto) operator()(Pred&& pred, State&& state, F&& f) const;
  59. };
  60. constexpr while_t while_{};
  61. #endif
  62. BOOST_HANA_NAMESPACE_END
  63. #endif // !BOOST_HANA_FWD_WHILE_HPP