C++ Logo

std-proposals

Advanced search

[std-proposals] enum with non-integral types

From: Filip <fph2137_at_[hidden]>
Date: Fri, 14 Mar 2025 17:27:13 +0100
Hi everyone, in other thread “lambdas in enums”
I have talked about enums having different underlying type.
I’m starting a new thread to organise the features.

------

I would like to use enums with different underlying type.
I have considered using inheritance style list:

```
enum struct Foo : int, float {
 A {3, 0.f},
 B { …, 1.f} // `…` to indicate that following element should follow increment from previous value
};
```

However it does pose several problems so other possibility is to use only one type and an initializer list.
Type used should have:
+ constexpr constructor,
+ constexpr default constructor
+ constexpr operator ++ const [if the values are not provided, could be explicitly deleted to force user to implement all values]

```
enum struct Foo : someType {
 A{ ~initializer list for `someType`~ },
 B, // by default it calls ++ operator on previous enum value
 C = default, // this could be an explicit way to show that enum generation should increment the value
 D {} // default initialisation of someType
};
```

The second option of new syntax allows us to easily access different values that we want to store.
```
Foo::A // is equivalent to static constexpr someType
```

Additional concerns:
+ sizeof(Foo) should be consistent with current enum sizeof functionality and return sizeof(someType).
+ initializer list is probably the only way to have multiple values per symbol
+ different enum values are not compared to each other when created not now so should not be with custom objects.
+ template enum struct should be possible in the future:

```
template <typename T, typename U>
enum struct Bar : std::pair<T, U>{};
```

Received on 2025-03-14 16:27:24