Why doesn't to!MyEnumType(42) work

cal callumenator at gmail.com
Mon Jul 16 14:04:49 PDT 2012


On Monday, 16 July 2012 at 21:00:00 UTC, cal wrote:
> On Monday, 16 July 2012 at 20:22:12 UTC, Era Scarecrow wrote:
>> You can convert a enum to an int, but not the other way around.
> [...]
>> MyEnumType z = cast(MyEnumType) 100; //Error: an int is not an 
>> enum! (Even if it's a valid match)
>
> I do this all the time....
>
> enum E
> {
>  A = 100,
>  B = 200
> }
>
> E e = cast(E) 100 ; // works fine...
>
> Maybe I misunderstood?
>
> The problem is that here int is not _implicitly_ convertible to 
> the enum. For example, if you write your own template without 
> any constraints:
>
> T to(T, S)(S i)
> {
>     return cast(T)i;
> }
>
> then:
> E e = to!E(100); // works, but is not safe

actually ignore that bit about implicit conversion, that's 
probably not "the problem"




More information about the Digitalmars-d mailing list