C++ Logo

std-proposals

Advanced search

Re: [std-proposals] enum with non-integral types

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Fri, 14 Mar 2025 19:37:44 +0000
What would be the benefit of this over let’s say... regular named constants?


________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]pp.org> on behalf of Filip via Std-Proposals <std-proposals_at_[hidden]>
Sent: Friday, March 14, 2025 5:34:55 PM
To: std-proposals_at_[hidden] <std-proposals_at_lists.isocpp.org>
Cc: Filip <fph2137_at_[hidden]>
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_at_lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-03-14 19:37:48