Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-09-29 20:45:34


"Noel Belcourt" <kbelco_at_[hidden]> writes:

> Hi,
>
> I'm new to the list so this may have been answered elsewhere (like a
> FAQ for MPL) but I couldn't find it.
>
> I've got 2 vector_c types like this
>
> typedef vector_c<int, 1,2,1,1,-2,0,1> p1;
> typedef vector_c<int, 0,3,0,-1,1,3,5> p2;
>
> and a resultant type defined as
>
> typedef vector_c<int, 1,1,0,1,1,0,1> r;

Resultant from what?

> that I want to compare at compile time using logic like this
>
> r = p1 || p1 && p2

That would be equivalent to r = p1. Why go to all that trouble? Or
do you mean

   r == p1 || p1 && p2

??

Anyway, what does it mean to use the logical && operator on two
sequences? Do you want elementwise comparison? Do you want the same
for r == p1? The usual meaning is to compare all elements and produce
a scalar boolean, which would then be incompatible with the vector of
bools produced by p1 && p2. Maybe you mean something like;

      mpl::transform<
          mpl::transform<r,p1,mpl::equal_to<_,_> >::type
        , mpl::transform<p1,p2,mpl::and_<_,_> >::type
        , mpl::or_<_,_>
>::type

??

> This pseudo code is what I want to do but I know this doesn't work.
> Can anyone can suggest a better approach?
>
> template <class R, class P1, class P2>
> struct validate_checkin : boost::mpl::equal<
> boost::mpl::placeholders::_1,
> boost::mpl::or_<
> boost::mpl::placeholders::_2,
> boost::mpl::and_<
> boost::mpl::placeholders::_2,
> boost::mpl::placeholders::_3
> >,
> >,
> >
> {};

Ugh; do yourself a favor and apply a couple of namespace aliases at
least; the whole point of those placeholders is that they're supposed
to increase readability!

Simplifying:

  namespace mpl = boost::mpl;
  using namespace mpl::placeholders;

  template <class R, class P1, class P2>
  struct validate_checkin
    : mpl::equal<
       _1, mpl::or_<_2, mpl::and_<_2,_3>, >,
>
  {};

Now I can at least begin to see it! Okay, that's a simple syntax
error to begin with. In C++

>, >

is never a valid token sequence, and you're doing it twice.

It seems to me that you also have several conceptual problems here.
First, you have perfectly good metafunction parameters here that you
can use; what are the placeholders supposed to be doing? Placeholders
only have an effect in a context where an MPL placeholder expression
can be used, e.g. in an argument to mpl::apply<...>. Why not just use
R, P1, and P2 directly?

Second, you seem to be trying to use the equal algorithm, which
operates on sequences, on the results of some invocation of mpl::or_,
which does not return a sequence but a scalar bool-valued MPL integral
constant.

-- 
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