Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2007-02-03 02:39:14


Steven Watanabe wrote:
> AMDG
>
> Is there any interest in a function template that generates a switch
> statement?
>
> struct f {
> typedef void result_type;
> void operator()(mpl::int_<0>) const {
> std::cout << "2" << std::endl;
> }
> void operator()(mpl::int_<1>) const {
> std::cout << "1" << std::endl;
> }
> void operator()(mpl::int_<5>) const {
> std::cout << "0" << std::endl;
> }
> };
>
> int main() {
> typedef mpl::vector<mpl::int_<0>, mpl::int_<1>, mpl::int_<5> > cases;
> switch<cases>(5, f()); //prints 0
> try {
> switch<cases>(3, f());
> } catch(bad_switch&) {}
> }

I'm interested. I'll need polymorphic return type handling though.
Something like:

struct f
{
     template <typename Case> struct result;

     template <> struct result<mpl::int_<0> >
         : mpl::identity<char> {};

     template <> struct result<mpl::int_<2> >
         : mpl::identity<float> {};

     template <> struct result<mpl::int_<5> >
         : mpl::identity<std::string> {};

     char operator()(mpl::int_<0>) const;
     float operator()(mpl::int_<1>) const;
     std::string operator()(mpl::int_<5>) const;
};

Regards,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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