Boost logo

Boost :

From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2008-03-07 08:37:05


On Thu, Mar 6, 2008 at 8:12 PM, vicente.botet <vicente.botet_at_[hidden]> wrote:
> From: "Steven Watanabe" <watanabesj_at_[hidden]>
> > AMDG
> >
> > vicente.botet wrote:
> >> Hello,
> >>
> >> I needs calculate the mlp::or_ of a sequence of nullary logical
> >> metafunction.
> >>
> >> This seams quite simple. I have do the following:
> >> <code>
> >> template <typename Seq>
> >> struct or_seq;
> >>
> >> template <typename Seq, long N>
> >> struct or_seq_aux : mpl::or_<
> >> typename mpl::front<Seq>::type,
> >> or_seq<typename mpl::pop_front<Seq>::type>
> >> > {};
> >>
> >> template <typename Seq>
> >> struct or_seq_aux<Seq, 1> : mpl::front<Seq>::type {};
> >>
> >> template <typename Seq>
> >> struct or_seq : or_seq_aux<Seq, mpl::size<Seq>::value> {};
> >> </code>
> >>
> >> Is there a boost or_seq like logical metafunction?
> >> Is there a better way to do it?
> >>
> >> I would be extremely interested in any suggestions.
> >>
> > not_<is_same<typename find_if<Seq, _>::type, typename end<Seq>::type> >?
> >
> > In Christ,
> > Steven Watanabe
>
> I have tested your much more compact solution and it works as expected.
> Here it is with the mpl namespace:
>
> template <typename Seq>
> struct or_seq : mpl::not_<
> boost::is_same<typename mpl::find_if<Seq, mpl::_>::type,
> typename mpl::end<Seq>::type>
> > {};
>

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
> {};

--
gpd

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