Return enum-templated struct based on runtime value

Sebastian Graf SebastianGraf at t-online.de
Sat Apr 6 05:43:56 PDT 2013


On Friday, 5 April 2013 at 18:00:56 UTC, bearophile wrote:
> Sebastian Graf:
>> I wonder if there is a good way to dry up this:
>
> I suggest to improve that code a lot.
>
> For machineWord there is:
>
> alias machineWord = Select!(machine == Machine.I386, uint, 
> ulong);
>
>
> This:
> (1 << 32)
>
> Gives:
> Error: shift by 32 is outside the range 0..31
>
> Generally it's better to use named enums and final switches, 
> where possible.
>
> Keep in mind built-in integral numbers have the max attribute.
>
> Bye,
> bearophile

Thanks, this shows that I didn't really try to run that example.
The Select template is helpful indeed.

Is there however some way to generate that switch with cases for 
each enum member in a template, so that each case calls 
<some-passed-template-func>(Machine)() instantiated with the 
appropriate enum member? I know you could do it with string 
mixins, but that be a hassle to implement...

Eventually, I want my code to look like

     void func(Machine machine)() { ... }
     switchOnEachEnumMember!func(machine);


More information about the Digitalmars-d-learn mailing list