Can Enums be integral types?

Bastiaan Veelo Bastiaan at Veelo.net
Sun Apr 17 18:25:32 UTC 2022


On Saturday, 16 April 2022 at 11:39:01 UTC, Manfred Nowak wrote:
> In the specs(17) about enums the word "integral" has no match. 
> But because the default basetype is `int`, which is an integral 
> type, enums might be integral types whenever their basetype is 
> an integral type.
>
> On the other hand the specs(7.6.5.3) about types say
> | A bool value can be implicitly converted to any integral type,
> | with false becoming 0 and true becoming 1.
>
> This seems senseless, when the enum has no names defined for 
> one of these values.

Not sure where the question is, but 
[6.5.3](https://dlang.org/spec/type.html#bool) makes that this 
works:
```d
int i = true; // 1
```
However this does not:
```d
enum E : int {Zero, One, Two}
E e1 = true; // Error: cannot implicitly convert expression 
`true` of type `bool` to `E`
E e2 = 1; // Error: cannot implicitly convert expression `1` of 
type `int` to `E`
```
The reason is in [17.1.5](https://dlang.org/spec/enum.html): 
“EnumBaseType types cannot be implicitly cast to an enum type.”

— Bastiaan.


More information about the Digitalmars-d-learn mailing list