Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2007-02-02 21:28:36


Tobias Schwinger <tschwinger <at> isonews2.com> writes:

>
> Hi Steven,
>
> Steven Watanabe wrote:
> > AMDG
> >
> > Is there any interest in a function template that generates a switch
> > statement?
> >
>
> Yes. I'm interested.
>
> > 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&) {}
> > }
> >
>
> Is this an interface proposal or just some code to illustrate the idea?
>

Yes this was how the interface would look. I also have
a three parameter version to handle default

> I'd like to have templatized parameters and probably separate function
> objects for the cases rather than shape-shifting overloads...

I did it this way to allow usage like

template<class FusionSequence>
struct print_nth {
    template<class Index>
    void operator()(Index) const {
        std::cout << fusion::at<Index>(sequence) << std::endl;
    }
    print_nth(const FusionSequence& s) : sequence(s) {}
    const FusionSequence& sequence;
};

>
> Regards,
> Tobias
>


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