Blame view

3rdparty/boost_1_81_0/boost/hana/functional/fix.hpp 2.73 KB
63e88f80   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  /*!
  @file
  Defines `boost::hana::fix`.
  
  Copyright Louis Dionne 2013-2022
  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   */
  
  #ifndef BOOST_HANA_FUNCTIONAL_FIX_HPP
  #define BOOST_HANA_FUNCTIONAL_FIX_HPP
  
  #include <boost/hana/config.hpp>
  #include <boost/hana/detail/create.hpp>
  
  #include <utility>
  
  
  namespace boost { namespace hana {
      //! @ingroup group-functional
      //! Return a function computing the fixed point of a function.
      //!
      //! `fix` is an implementation of the [Y-combinator][], also called the
      //! fixed-point combinator. It encodes the idea of recursion, and in fact
      //! any recursive function can be written in terms of it.
      //!
      //! Specifically, `fix(f)` is a function such that
      //! @code
      //!     fix(f)(x...) == f(fix(f), x...)
      //! @endcode
      //!
      //! This definition allows `f` to use its first argument as a continuation
      //! to call itself recursively. Indeed, if `f` calls its first argument
      //! with `y...`, it is equivalent to calling `f(fix(f), y...)` per the
      //! above equation.
      //!
      //! Most of the time, it is more convenient and efficient to define
      //! recursive functions without using a fixed-point combinator. However,
      //! there are some cases where `fix` provides either more flexibility
      //! (e.g. the ability to change the callback inside `f`) or makes it
      //! possible to write functions that couldn't be defined recursively
      //! otherwise.
      //!
      //! @param f
      //! A function called as `f(self, x...)`, where `x...` are the arguments
      //! in the `fix(f)(x...)` expression and `self` is `fix(f)`.
      //!
      //! ### Example
      //! @include example/functional/fix.cpp
      //!
      //! [Y-combinator]: http://en.wikipedia.org/wiki/Fixed-point_combinator
  #ifdef BOOST_HANA_DOXYGEN_INVOKED
      constexpr auto fix = [](auto&& f) {
          return [perfect-capture](auto&& ...x) -> decltype(auto) {
              return forwarded(f)(fix(f), forwarded(x)...);
          };
      };
  #else
      template <typename F>
      struct fix_t;
  
      BOOST_HANA_INLINE_VARIABLE constexpr detail::create<fix_t> fix{};
  
      template <typename F>
      struct fix_t {
          F f;
  
          template <typename ...X>
          constexpr decltype(auto) operator()(X&& ...x) const&
          { return f(fix(f), static_cast<X&&>(x)...); }
  
          template <typename ...X>
          constexpr decltype(auto) operator()(X&& ...x) &
          { return f(fix(f), static_cast<X&&>(x)...); }
  
          template <typename ...X>
          constexpr decltype(auto) operator()(X&& ...x) &&
          { return std::move(f)(fix(f), static_cast<X&&>(x)...); }
      };
  #endif
  }} // end namespace boost::hana
  
  #endif // !BOOST_HANA_FUNCTIONAL_FIX_HPP