Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2003-02-24 17:12:25


Markus Werle <yg-boost-users_at_[hidden]> writes:

> Hi!
>
> I am trying to migrate some parts of my code from
> loki to boost::mpl.
>
> Q1: which is the best way to perform nested if-then-else?
>
> Assume given "values":
> bool B1, bool B2;
> class ff, class tf, class ft, class tt;
>
>
> I replaced
>
> -----------------------------------------------------------
> typedef typename
> Loki::Select<B1,
> typename Loki::Select<B2, tt, tf>::Result,
> typename Loki::Select<B2, ft, ff>::Result
> >::Result type;
> -----------------------------------------------------------
>
> with
>
>
> typedef typename
> apply_if
> <
> bool_c<B1>,
>
> apply_if
> <
> bool_c<B2>,
> identity<tt>,
> identity<tf>
> >,
>
> apply_if
> <
> bool_c<B2>,
> identity<ft>,
> identity<ff>
> >
> >::type type;

    typedef typename
    mpl::apply_if_c<
        B1
      , mpl::if_c<B2, tt, tf>
      , mpl::if_c<B2, ft, ff>
>::type

is the most economical form, given what you have to work with.

> Q2: Is apply_if always to be preferred over if_?

No

    apply_if<C, identity<X>, identity<Y> >

is a lot less clear and less-efficient than simply:

    if_<C, X, Y>

> Q3: (OT) Is there a mpl-equivalent to run-time case?

:-)

Aleksey and I have been discussing some shorthands for

  if (c1) then t1
  else if (c2) then t2
  else if (c3) then t3
  ...
  else if (c4) then t4

but that's not quite the same as a case statement.

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