enums and std.traits
deadalnix
deadalnix at gmail.com
Mon Aug 6 08:39:35 PDT 2012
Le 04/08/2012 23:09, Jonathan M Davis a écrit :
> At present (though it didn't used to work this way until about 4 months ago -
> the change is in 2.060), a number of std.traits templates - including
> isSomeString - evaluate to false for enums. So,
>
> enum E : string { a = "hello" }
> static assert(isSomeString!(E.a));
>
> will fail. This is in spite of the fact that
>
> auto func(string a) {...}
>
> will accept E.a without complaint. So, if you change
>
> auto func(string a) {...}
>
> to
>
> auto func(T)(T a) if(isSomeString!T) {...}
>
[...]
>
> - Jonathan M Davis
E;A is a string. By definition. So isSomeString must be true.
However,
T foo(T)(T t) if (isSomeString!T) {
return t ~ t[0];
}
must trigger an error when T is an enum type. if t has type E, which
implicitly cast to string, t ~ t[0] must have type string and not E.
So the given sample code should trigger a compile time error about
inconsistent return type.
The same goes for other enums operations.
More information about the Digitalmars-d
mailing list