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

Timoses timosesu at gmail.com
Fri Apr 27 13:43:47 UTC 2018


On Friday, 27 April 2018 at 13:39:22 UTC, Simen Kjærås wrote:
> That's an unfortunate error message. The problem is TempStruct 
> is defined inside the Temp template. In the same way that 
> struct Foo(T) {} is different for Foo!int and Foo!string, 
> TempStruct is a different type for Temp!(menum.A) and 
> Temp!(menum.B).
>
> The solution is to move TempStruct outside the template:
>
> struct TempStruct { uint i; }
>
> template Temp(menum e)
> {
>     TempStruct Temp;
>     shared static this()
>     {
>         static if (e == menum.A)
>             Temp.i = 3;
>     }
> }
>
> --
>   Simen

Ty.
I figured that's the reason. I still can't quite get my head 
around "Why?" though.

`instantiateWith` gets called in three variations (menum.A, 
menum.B and menum.C). This causes instantiateWith to return 
TempStruct for each case of Temp...

However, I was under the impression that a templated function 
will exist multiple (in this case 3) times, so the return type 
should be allowed to be different?!


More information about the Digitalmars-d-learn mailing list