|
Boost : |
Subject: Re: [boost] [mpl] multiset
From: Eric Niebler (eniebler_at_[hidden])
Date: 2015-03-12 22:48:23
On 3/12/2015 5:05 PM, Louis Dionne wrote:
> Eric Niebler <eniebler <at> boost.org> writes:
>> [...]
>>
>> 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>>;
>>
>> Metafunctions? We don't need no stinkin' metafunctions!
>
> FWIW, here's how you can implement a factorial with lazy
> metafunctions, here done with MPL11:
>
> template <typename N>
> struct fact
> : if_c<N::value == 0,
> ullong<1>,
> mult<N, fact<pred<N>>>
> >
> { };
>
> Lambda expressions are a nice hammer, but a factorial metafunction
> is seemingly not a nail.
Ha! That works in Meta too:
using namespace meta;
template<typename N>
struct factorial : eval<
if_c<N::value == 0,
meta::size_t<1>,
lazy::multiplies<N, factorial<dec<N>>>>>
{};
But now that I think about it, there *is* a difference between MPL-style
lazy metafunctions and meta::lazy that could complicate these uses of
Meta. The metafunctions in meta::lazy don't assume their arguments are
themselves lazy metafunctions, which would make lazy metafunction
composition difficult. It's really just not the Meta way.
-- Eric Niebler Boost.org http://www.boost.org
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk