Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2003-10-19 19:27:24


This is something I had been working on about two months ago but didn't
post to the sandbox until recently.

The basic idea is this:

   switch_( [variant] )
     |= case_< [pattern1] >(...)
     |= case_< [pattern2] >(...)
     :::
     |= default_(...) // optional catch-all
     ;

The switch_ will fail to compile if not every case is handled. In terms
of handling, the case_ constructors take typical function objects,
though the switch_ ignores any return values.

An example usage is the following (though I have again left out the
function objects that need to be passed to the case_ constructors):

   using namespace boost::type_switch;

   variant<
       int
     , pair<int,double>
     , pair<int,int>
     , pair<double,int>
> var;

   switch_(var)
     |= case_< pair<_1,_1> >(...) // matches pair<int,int>
     |= case_< pair<int,_> >(...) // matches pair<int,*>
     |= case_< pair<_,_> >(...) // matches pair<*,*>
     |= case_< int >(...)
     ;

The pattern matching is implemented in terms of lambda_match, which I
have also added to the sandbox (to boost/mpl). Notably, lambda_match
leverages the MPL lambda workarounds for deficient compilers, extending
the applicability of type_switch somewhat.

I'd appreciate any feedback on syntax or semantics. My guess is that
this will appear in the 1.32 release unless feedback is overwhelmingly
positive.

Thanks,
Eric


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk