Opened 16 years ago

Last modified 12 years ago

#799 new Feature Requests

mpl::if_

Reported by: nobody Owned by: Joel Falcou
Milestone: Boost 1.46.0 Component: mpl
Version: Boost 1.45.0 Severity: Problem
Keywords: Cc: joel.falcou@…

Description (last modified by Dave Abrahams)

There is an alternative implementation of mpl::if.
This implementation makes code clearer.

We can write:
typename if_
<
  X,
  Y
>
::else_if
<
  Z,
  U
>
::else_
<
  V
>::type

The implementation can be found here:
http://cbear.berlios.de/meta/if.hpp

Change History (12)

comment:1 by Dave Abrahams, 15 years ago

Description: modified (diff)
Resolution: Noneinvalid
Severity: Showstopper
Status: assignedclosed

I disagree; it's far too verbose, and thus less clear, when you consider the need for the template keyword, which you left out.

  typename if_
  <
    X,
    Y
  >
  ::template else_if
  <
    Z,
    U
  >
  ::template else_
  <
    V
  >::type

in reply to:  1 comment:2 by anonymous, 15 years ago

Resolution: invalid
Status: closedreopened

Replying to dave:

I disagree; it's far too verbose, and thus less clear, when you consider the need for the template keyword, which you left out.

Ok, lets compare:

1 condition:

// Boost
typedef Mpl::if_<C, T, E>::type X;

// cbear
typedef Meta::if_<C, T, E>::type X;

5 conditions:

// Boost
template<class C1, class C2, class C3, class C4, class C5>
struct X: 
        Mpl::if_<C1, T1, 
        typename Mpl::if_<C2, T2, 
        typename Mpl::if_<C3, T3, 
        typename Mpl::if_<C4, T4, 
        typename Mpl::if_<C5, T5, 
        E
        >::type 
        >::type 
        >::type 
        >::type 
        >
{
};

// cbear
template<class C1, class C2, class C3, class C4, class C5>
struct X:
        Meta::if_<C1, T1>::
        template else_if<C2, T2>::
        template else_if<C3, T3>::
        template else_if<C4, T4>::
        template else_if<C5, T5>::      
        template else_<E>
{
};

enable if T is integer:

// Boost
template<class T>
T foo(T t, typename boost::enable_if<boost::is_integral<T> >::type* dummy = 0);

// cbear
template<class T>
T foo(T t, typename Meta::if_<boost::is_integral<T> >::type* dummy = 0);

enable if T is integer (returns T) or pointer (returns int):

// Boost
template<class T>
typename Mpl::lazy_enable_if<
        Mpl::or_<boost::is_integral<T>, boost::is_pointer<T> >, // !!!
        Mpl::if_<boost::is_integral<T>, T, int> >::type
foo(T t);

// cbear
template<class T>
typename Meta::if_<boost::is_integral<T>, T>::
template else_if<boost::is_pointer<T>, int>::type
foo(T t);

I would agree with some other arguments. But not with 'it's far too verbose, and thus less clear'. That's false!

comment:3 by Dave Abrahams, 15 years ago

Owner: changed from nobody to Aleksey Gurtovoy
Status: reopenednew

Sorry, I hadn't looked at your implementation, so I assumed that you had to use else_ rather than a third argument. I'm beginning to like your suggestion. We'll have to see what Aleksey says.

comment:4 by Aleksey Gurtovoy, 15 years ago

Status: newassigned

It's a nice extension, I like it. Let's get it in.

comment:5 by sergey.junk@…, 15 years ago

Note: it also replaces lazy_enable_if (if_), and enable_if (if_c).

in reply to:  5 ; comment:6 by Dave Abrahams, 15 years ago

Replying to sergey.junk@comrades.id.ua:

Note: it also replaces lazy_enable_if (if_), and enable_if (if_c).

I know, and I like the way it does that, too.

in reply to:  6 comment:7 by anonymous, 15 years ago

Replying to dave:

Replying to sergey.junk@comrades.id.ua:

Note: it also replaces lazy_enable_if (if_), and enable_if (if_c).

I know, and I like the way it does that, too.

In case berlios.de dosn't work, see http://cbear.svn.sourceforge.net/viewvc/cbear/trunk/cbear.berlios.de/meta/if.hpp?revision=8&view=markup&pathrev=13

comment:8 by Dave Abrahams, 15 years ago

Component: Nonempl

comment:9 by Daniel James, 13 years ago

Severity: ShowstopperProblem

comment:10 by anonymous, 12 years ago

Owner: changed from Aleksey Gurtovoy to Joel Falcou
Status: assignednew

comment:11 by Joel Falcou, 12 years ago

Cc: joel.falcou@… added
Milestone: Boost 1.46.0
Version: NoneBoost 1.45.0

comment:12 by NN, 12 years ago

This is a very good alternative. I wish boost had this feature.

Note: See TracTickets for help on using tickets.