Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2007-03-17 00:27:55


on Fri Mar 16 2007, Scott Meyers <usenet-AT-aristeia.com> wrote:

> I'm trying to use the MPL for the first time, and I have two questions.
> First, I want to use BOOST_MPL_ASSERT to say that at least one of two
> conditions is true. For the time being, one of the conditions is always
> false, so this is what I've tried:
>
> BOOST_MPL_ASSERT((
> false
> ||
> boost::is_same<CallerConstraints,
> CodeConstraints::Ignore_Constraints>
> ));
>
> It won't compile

Generally C++ gets unhappy with you when you try to apply arithmetic
operators to types. What you've done above is similar to

     BOOST_MPL_ASSERT((
       false
       ||
       int
     ))

Try this (untested):

     BOOST_MPL_ASSERT((
        boost::mpl::or_<
            boost::mpl::false_
          , boost::is_same<
                CallerConstraints, CodeConstraints::Ignore_Constraints
>
>
     ))

Two things to consider:

* Are you missing a typename before CodeConstraints, or is
  Ignore_Constraints not a dependent type in this context?
* Is is_same the appropriate test, or do you really want something
  like mpl::equal?

> , so I tried this:
>
> BOOST_MPL_ASSERT((
> mpl::or_(false,
> boost::is_same<CallerConstraints,
> CodeConstraints::Ignore_Constraints>)
> ));

mpl::or_ is a metafunction just like all the others. You need the
angle brackets.

> It won't compile either. I'm guessing that I have to use mpl::false_ or
> something in place of false, but that doesn't work either. Can somebody
> please tell me the proper way to say what I want to say?
>
> My second question is about the existence of an MPL algorithm akin
> to the STL algorithm includes. Given two MPL vectors V1 and V2, I
> want to know if all the types in V1 are also in V2. I know about
> mpl::contains, but I can't find mpl::includes. Is there one, or do
> I need to write it myself?
>
> I have the MPL book, so references to that are fine, in addition to
> anything online.

  // (untested)
  template <class OuterSequence, InnerSequence>
  struct includes
    : boost::is_same<
          typename mpl::find_if<
              InnerSequence
            , mpl::not_<
                  mpl::contains<OuterSequence, _1>
>
>::type
        , typename mpl::end<SubSequence>::type
>
  {};

In plain English, OuterSequence includes InnerSequence iff the result
of trying to "find an element in InnerSequence that's not contained in
OuterSequence" is the same as the InnerSequence's end iterator,
i.e. no such element can be found.

HTH,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net