Boost logo

Boost :

Subject: Re: [boost] Phoenix review
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2008-09-27 11:32:19


On Sat, Sep 27, 2008 at 6:23 AM, Joel de Guzman
<joel_at_[hidden]> wrote:
> Giovanni Piero Deretta wrote:
>
> [snip about the switch_]
>
>> Hum, what about relaxing a little the 'C++ in C++ tag' and making
>> switch_ more powerfull than the builtin switch?
>>
>> Instead of:
>>
>> switch_(arg1)
>> [
>> case_<1>(cout << val("one") << '\n'),
>> case_<2>(cout << val("two") << '\n'),
>> default_(cout << val("other value") << '\n')
>> ]
>>
>> do this:
>>
>> switch_(arg1)
>> [
>> case(1)[cout << val("one") << '\n'],
>> case(2)[cout << val("two") << '\n'],
>> default_[cout << val("other value"] << '\n')
>> ]
>>
>> this allows not only runtime selection of switch case values, but it
>> will also work with everything that is equally comparable:
>>
>> switch_(arg1)
>> [
>> case("one")[cout << val(1) << '\n'],
>> case("two")[cout << val(2) << '\n'],
>> default_[cout << val("other value"] << '\n')
>> ]
>>
>> which IMHO is a killer feature.
>
> Cool!
>
>> The fact that the case parameter is not a template parameter might
>> limit compiler optimizations unless the the compiler is good at
>> constant propagation, but there is a workaround:
>>
>> switch_(arg1)
>> [
>> case(mpl::int_<1>())[cout << val("one") << '\n'],
>> case(mpl::int_<2>())[cout << val("two") << '\n'],
>> default_[cout << val("other value"] << '\n')
>> ]
>>
>> Let's leave the ugly syntax to those that need it :)
>
> It seems your proposal is compatible with this syntax anyway:
>
> case<1>()[cout << val("one") << '\n'],
>
> So you can choose one or the other.
>
>> As another extension, when visiting heterogeneous sequences (think
>> fusion::for_each), a switch by type would be great:
>>
>> switch_(arg1)
>> [
>> case(type<std::string>())[cout << val("std::string") << '\n'],
>> case(type<int>()) [cout << val("int") << '\n'],
>> default_[cout << val("other type"] << '\n')
>> ]
>>
>> (in this case switch_ would need to know about type<> because it must
>> not compile the not-taken branches). Even better:
>>
>> switch_(arg1)
>> [
>> case(type<std::string>())[cout << val("std::string") << '\n'],
>> case(type<std::vector<_> >()) [cout << val("a vector of
>> something") << '\n'],
>> default_[cout << val("other type"] << '\n')
>> ]
>
> I love it! I think it's wonderful. Would you be interested in
> implementing this? :-P
>

Sure, I'll give it a try.

-- 
gpd

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