Boost logo

Boost :

Subject: Re: [boost] [mpl] is there a or_seq like logical metafunction?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-02-07 14:29:29


----- Original Message -----
From: "Larry Evans" <cppljevans_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Saturday, February 07, 2009 8:24 PM
Subject: Re: [boost] [mpl] is there a or_seq like logical metafunction?

> On 04/27/08 20:50, David Abrahams wrote:
>> on Fri Mar 07 2008, Steven Watanabe <watanabesj-AT-gmail.com> wrote:
>>
>>> AMDG
>>>
>>> Giovanni Piero Deretta wrote:
>>>> It seems a perfect application for fold:
>>>>
>>>> template<typename Seq>
>>>> struct or_seq
>>>> : mpl::apply<
>>>> mpl::fold<mpl::_1, mpl::false_, mpl::quote2<mpl::or_> >
>>>> , Seq
>>>> > {};
>>>>
>>>> template<typename Seq>
>>>> struct and_seq
>>>> : mpl::apply<
>>>> mpl::fold<mpl::_1, mpl::true_, mpl::quote2<mpl::and_> >
>>>> , Seq
>>>> > {};
>>>>
>>> Not quite, because we want it to short circuit.
>>> Not to mention that quote2 will not work on and_ and or_.
>>
>> Yeah, these should be rewritten in terms of mpl::find.
>> Not sure why mpl::apply was used here either, though maybe I'm missign
>> something.
>>
>
> The curren mpl::find uses and_ indirectly (in iter_fold_if.hpp:46).
> Would such a recursive implementation still work?
>
> Anyway, J.Fletcher is now attempting a variadic template rewrite of
> mpl:;find:
>
> http://sourceforge.net/mailarchive/message.php?msg_name=F0DEE5AE9F6027488C064CD31A5B26AC07BA6EDC78%40EXCHANGEMB.campus.aston.ac.uk
>
> which doesn't use and_; however, another alternative which does
> short-circuit is attached. It uses eval_if to short-circuit the
> evaluation. Fletcher's find, OTOH, used if_; however, I think
> that's just because he's not interested in optimization yet.
>
> A very similar or_ is done by simply exchanging the Zero and One
> parameters. Interestingly enough, the current and_ uses
> a similar pattern in that it recursively calls a template (and.hpp:23).
>
> The attached (with the similar or.hpp) passes the logical.cpp test.
 
 

Hi,

Wow, I didn't think that this thread was yet alive.

After suggestion of I don't remember who, I have implemented it as

/* Calculate the mlp::and_ of a sequence of nullary logical metafunctions.*/
template <typename Seq>
struct and_seq : boost::is_same<typename mpl::find_if<Seq, mpl::not_<mpl::_> >::type,
           typename mpl::end<Seq>::type>
     {};

/* Calculate the mlp::or_ of a sequence of nullary logical metafunctions. */
template <typename Seq>
struct or_seq : mpl::not_<boost::is_same<typename mpl::find_if<Seq, mpl::_>::type,
           typename mpl::end<Seq>::type>
> {};

If there are better ways to do that, you don't tink that this must be encapsulated in the library? Why not add these metafunctions in Boost.MPL?

Thanks,

Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk