ericniebler/meta

Integrate self-recursive lambdas

ericniebler opened this issue · 2 comments

namespace meta_detail
{
    template<typename Ts, int = 0>
    struct lambda_rec_;

    template<typename ...Ts>
    struct lambda_rec_<list<Ts...>>
    {
        template<typename...Us>
        using apply = apply<let<var<_self, lambda_rec_>, lambda<Ts...>>, Us...>;
    };
}

////////////////////////////////////////////////////////////////////////////////////
// lambda_rec
/// For defining a self-recursive lambda. In the body of a lambda, the placeholder
/// `_self` refers to the nearest enclosing `lambda_rec`
///
/// \code
/// // Define a factorial template in terms of a recursive lambda
/// using factorial_ = lambda_rec<_a,
///     lazy::if_<lazy::greater<_a, meta::size_t<0>>,
///               lazy::multiplies<_a, lazy::apply<_self, lazy::dec<_a>>>,
///               meta::size_t<1>>>;
/// template<std::size_t N>
/// using factorial = apply<factorial_, meta::size_t<N>>;
///
/// static_assert(factorial<0>::value == 1, "");
/// static_assert(factorial<1>::value == 1, "");
/// static_assert(factorial<2>::value == 2, "");
/// static_assert(factorial<3>::value == 6, "");
/// static_assert(factorial<4>::value == 24, "");
/// \endcode
template<typename ...Ts>
using lambda_rec = meta_detail::lambda_rec_<list<Ts...>>;

Is this in range-v3 master already?

Use the source, Luke.