|
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:
mpl::not_<X>::type
is
mpl::bool_<!X::value>
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
false_::type
is
false_
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::apply_if<
expr1
, 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.
HTH,
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk