File size: 1,958 Bytes
8ae5fc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#ifndef METAL_LAMBDA_BIND_HPP
#define METAL_LAMBDA_BIND_HPP

#include "../config.hpp"

namespace metal {
/// \cond
namespace detail {
    template <class lbd, class... vals>
    struct _bind;
}
/// \endcond

/// \ingroup lambda
///
/// ### Description
/// Provides higher-order composition of \lambdas.
///
/// \tip{Use metal::arg<n> as a placeholder for the n-th argument.}
///
/// ### Usage
/// For any \lambdas `lbd` and `lbd_0, ..., lbd_n-1`
/// \code
///     using result = metal::bind<lbd, lbd_0, ..., lbd_n-1>;
/// \endcode
///
/// \returns: \lambda
/// \semantics:
///     If `lbd` holds \expression `f` and, likewise, `lbd_0, ..., lbd_n-1`
///     hold \expressions `f_0, ..., f_n-1`, then
///     \code
///         using result = metal::lambda<g>;
///     \endcode
///     where `g` is an \expression such that
///     \code
///         template<class... args>
///         using g = f<f_0<args...>, ...<args...>, f_n-1<args...>>;
///     \endcode
///
/// ### Example
/// \snippet lambda.cpp bind
///
/// ### See Also
/// \see lambda, invoke, arg, always
template <class lbd, class... vals>
using bind = typename detail::_bind<lbd, vals...>::type;
}

#include "../detail/sfinae.hpp"
#include "../lambda/lambda.hpp"

namespace metal {
/// \cond
namespace detail {
    template <class... vals>
    struct _bind_impl {
        template <template <class...> class expr, template <class...> class... params>
        using type =
#if defined(METAL_WORKAROUND)
            call<expr, call<params, vals...>...>;
#else
            expr<params<vals...>...>;
#endif
    };

    template <class lbd, class... vals>
    struct _bind {
    };

    template <template <class...> class expr, template <class...> class... params>
    struct _bind<lambda<expr>, lambda<params>...> {
        template <class... vals>
        using impl = forward<_bind_impl<vals...>::template type, expr, params...>;

        using type = lambda<impl>;
    };
}
/// \endcond
}

#endif