Thread overview
Can Enums be integral types?
Apr 16, 2022
Manfred Nowak
Apr 17, 2022
Bastiaan Veelo
Apr 19, 2022
Era Scarecrow
Apr 19, 2022
Bastiaan Veelo
Apr 19, 2022
Era Scarecrow
Apr 20, 2022
Manfred Nowak
April 16, 2022

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.

April 17, 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 makes that this works:

int i = true; // 1

However this does not:

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: “EnumBaseType types cannot be implicitly cast to an enum type.”

— Bastiaan.

April 19, 2022

On Sunday, 17 April 2022 at 18:25:32 UTC, Bastiaan Veelo wrote:

>

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.

The reason is in 17.1.5: “EnumBaseType types cannot be implicitly cast to an enum type.”

The 'integral' or numeric value is used for uniqueness, not for math or some other effect, anymore than a primary int key in a SQL database is used to identify someone's birthday. (Maybe that's the wrong analogy, comparing apples to oranges perhaps).

We will indeed have to explicitly cast to get around it, though it doesn't mean much. If you have say true=1, blue=2, what is blue+true? Numerically it's 3 but there's no value 3, or value 3 could be say potato...

Few years ago i made an enum type flag/library storage library, which would take an int and convert to N flags, or N flags to an int for compactly storing said values. But it's been quite a while, though i do recall a lot of casting and binary AND/OR/XOR's involved for it to work the way it was intended.

April 19, 2022

On Tuesday, 19 April 2022 at 01:25:13 UTC, Era Scarecrow wrote:

>

The 'integral' or numeric value is used for uniqueness, […]

There is nothing that requires enum values to be unique, though:

import std;
void main()
{
    enum E {Zero = 0, One = 0, Two = 0}
    writeln(E.Two); // Zero!
}

— Bastiaan.

April 19, 2022

On Tuesday, 19 April 2022 at 13:20:21 UTC, Bastiaan Veelo wrote:

>

There is nothing that requires enum values to be unique, though:

import std;
void main()
{
    enum E {Zero = 0, One = 0, Two = 0}
    writeln(E.Two); // Zero!
}

True, but if you want it be useful they really need to be unique.

April 20, 2022

On Sunday, 17 April 2022 at 18:25:32 UTC, Bastiaan Veelo wrote:

>

The reason is in 17.1.5: “EnumBaseType types cannot be implicitly cast to an enum type.”

Thy. That's the anchor in the specs preventing Enums to be integral types.