What would be the benefit of this over let’s say... regular named constants?

 

 


From: Std-Proposals <std-proposals-bounces@lists.isocpp.org> on behalf of Filip via Std-Proposals <std-proposals@lists.isocpp.org>
Sent: Friday, March 14, 2025 5:34:55 PM
To: std-proposals@lists.isocpp.org <std-proposals@lists.isocpp.org>
Cc: Filip <fph2137@gmail.com>
Subject: [std-proposals] enum with non-integral types



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>{};
```
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals