|
Boost : |
Subject: Re: [boost] [mpl] multiset
From: Eric Niebler (eniebler_at_[hidden])
Date: 2015-03-12 18:55:00
On 3/12/2015 1:47 PM, pfultz2 wrote:
>> I never need to use helper metafunctions. Meta's expression evaluator
>> handles laziness.
>
> Yes you do use helper metafunctions. Look at your implementation of fold.
You have a point. There are lots of dirty tricks in the implementation
of Meta to work around language shortcomings and improve compile-times.
That's not generally how Meta is *used*, but when I said "never" I was
being too strong.
> With
> a library built with full laziness, you should be able to build a SFINAE-
> friendly fold like this:
>
> template<class Iterable, class State, class Fun>
> using fold = eval_if<empty<Iterable>,
> State,
> fold<pop_front<Iterable>, lazy_apply<Fun, State, front<Iterable>>,
> Fun>
> ;
Template aliases cannot be self-referential. The alias isn't in scope
until the semicolon. It's a bit of a bummer.
> Is something like this possible with Meta library?
Sadly not, at least not right now. For some things, like recursion and
specialization, there's no escaping class templates, and hence
metafunctions. That's OK, Meta works fine with metafunctions; just use
meta::lazy as if it were the MPL. The inconsistency bugs me though.
I'd very much like to add support for recursive lambdas. I can imagine
the factorial example looking like this:
using factorial_ = lambda_rec<_a,
lazy::if_c<lazy::greater<_a, meta::size_t<0>>,
lazy::multiplies<N, lazy::apply<_self, lazy::dec<_a>>> ,
meta::size_t<1>>>;
template<std::size_t N>
using factorial = apply<factorial_, meta::size_t<N>>;
The _self placeholder recurses to the nearest enclosing lambda_rec. It
shouldn't be too hard to implement, actually. A recursive fold could be
implemented this way (not that it would, necessarily).
And some day I'd like to implement let_rec, which would let you perform
evaluations in terms of two or more mutually recursive lambdas. But
that's crazy-town, and I don't see a huge need for it.
-- 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