Template to retrieve compile-time enum member from run-time enum member?
Timoses
timosesu at gmail.com
Fri Apr 27 13:27:45 UTC 2018
Bumped across another problem : /
```
import std.stdio;
enum menum { A, B, C }
void main()
{
foo(menum.A);
}
void foo(menum e)
{
writeln(instantiateWith!Temp(e));
}
auto instantiateWith(alias Fn, T)(T x)
if (is(T == enum))
{
switch (x)
{
import std.traits : EnumMembers;
static foreach (e; EnumMembers!T)
case e:
return Fn!e;
default:
assert(false);
}
}
template Temp(menum e)
{
struct TempStruct { uint i; };
TempStruct Temp;
shared static this()
{
static if (e == menum.A)
Temp.i = 3;
}
}
```
now returns:
source\app.d(25,17): Error: mismatched function return type
inference of `TempStruct` and `TempStruct`
source\app.d(12,33): Error: template instance
`app.instantiateWith!(Temp, menum)` error instantiating
dmd failed with exit code 1.
It's the same return type... so why the error?
More information about the Digitalmars-d-learn
mailing list