Template to retrieve compile-time enum member from run-time enum member?

Timoses timosesu at gmail.com
Fri Apr 27 10:28:02 UTC 2018


On Thursday, 26 April 2018 at 16:46:11 UTC, Simen Kjærås wrote:
> The only step you're missing is the template needs to be 
> instantiated inside the static foreach, like this:
>
> auto instantiateWith(alias Fn, T)(T x)
> if (is(T == enum))
> {
>     import std.traits : EnumMembers;
>     switch (x)
>     {
>         static foreach (e; EnumMembers!T)
>             case e:
>                 return Fn!e;
>         default:
>             assert(false);
>     }
> }
>
> enum menum { A, B, C }
>
> template Temp(menum m)
> {
>     enum Temp = m.stringof;
> }

Ah thanks!!
I struggled a bit with finding out how to actually it in my use 
case. I just didn't see the pattern:

```
instantiateWith!<ANY-Template-That-should-be-instantiated-with-run-time-Var>(<run-time-var>)
```

Thank you!


More information about the Digitalmars-d-learn mailing list