Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-03-31 14:43:09


Bruce Trask <Bruce.Trask_at_[hidden]> writes:

> Hi,
>
> I coded up the mpl::map versus the SWITCH approach and I am thinking
> that the SWITCH approach is better in this case as it shows the various
> mnemonic choices at the point of the SWITCH. Thoughts?

You can't meaningfully compare the syntax of the map with something
that has unspecified syntax (i.e. switch) ;-)

In MPL, the closest thing to switch is currently spelled:

   eval_if<
       is_same<c1,x>
     , identity<r1>
     , eval_if<
           is_same<c2,x>
         , identity<r2>
         ...

         , eval_if<
               is_same<cn,x>
             , identity<rn>
             , identity<default_>
>
>
>
         
> Also if the SWITCH is the better way to go, it leads me to my original
> question as to whether there is a mpl::switch?
>
> enum tagit
> {
> one,
> two,
> three
> };
>
> struct First
> {
> static void func()
> {
> cout << "First" << endl;
> }
> };
>
> struct Second
> {
> static void func()
> {
> cout << "Second" << endl;
> }
> };
>
> struct Third
> {
> static void func()
> {
> cout << "Third" << endl;
> }
> };
>
> typedef mpl::map<
> mpl::pair<mpl::integral_c<tagit, one>, First>,
> mpl::pair<mpl::integral_c<tagit, two>, Second>,
> mpl::pair<mpl::integral_c<tagit, three>, Third>
> > funcchooser;

Ehm, if that's all you need to do:

template <int> func_;

template <>
struct func_<1>
{ static void func() { cout << "First" << endl }};

template <>
struct func_<2>
{ static void func() { cout << "Second" << endl }};

template <>
struct func_<3>
{ static void func() { cout << "Third" << endl }};

template<tagit ti>
void funcCaller()
{
  func_<ti>::func();
}
  

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