Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-09-05 15:09:06

Jaakko Jarvi <jajarvi_at_[hidden]> writes:

> Hi,
> We're writing enable_if docs and have a question about MPL. The docs
> do not seem to mention metafunctions like not_ or and_; we can find
> logical_not, logical_and, etc. which seem to be applying to functions,
> not values.
> So assuming we have:
> class A { static const bool value = true; };
> What is the right way to get the inverse of A::value as an MPL wrapped
> boolean?
> Is it boost::mpl::not_<A> or something else?

Yes, specializations of mpl::not_<A>, and of all of MPL's logical
metafunctions, are valid boolean constant wrappers. Those logical
metafunctions are derived from their return value, so:




That's makes writing:

       mpl::if_<mpl::not_<X>, a, b>::type

legal, which is nicer than:

       mpl::if_<typename mpl::not_<X>::type, a, b>::type

Note also that the integral constant wrappers are nullary
metafunctions, so



The other advantage to using mpl::not_<X> rather than
mpl::not_<X>::type is that it can delay the instantiation of X when X
is a template specialization:

        , mpl::not_<complicated_bool_metafunction<X> >
        , mpl::true_
>::type z;

if expr1 turns out to be mpl::false_, the complicated_metafunction
will never be instantiated.


Dave Abrahams
Boost Consulting

Boost list run by bdawes at, gregod at, cpdaniel at, john at