C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Enum class extensions

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Wed, 23 Feb 2022 22:50:18 +0100
śr., 23 lut 2022 o 11:12 Dragan Grbic via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> Proposal:
> Add static member accessors (generated by compiler) to the enum class/struct to enable iteration over enum.
>
> enum class Foo {
> a,
> b = 3,
> c = b,
> d
> }
>
> for (auto e: Foo::enum::values) {
> std::cout << e;
> }
>
> X::enum::values should return the static const array of underlying type filled with values from enum ordered as in enum definition. For example above, it would be [0, 3, 3, 4].
> If X::enum::values isn't used, the compiler does not generate the array.
>
> Also, X::enum::names could be added with similar semantics, except that it would be of type array<char*, N> (where N is count of enum elements).
> For the above example it would be ["a", "b", "c", "d"].
>

Most of this will be possible with reflection, another thing if the
committee would like to have this before reflection your syntax is
uncommon for C++
Correct solution should look like `std::enum_values<Foo>()` and I
recall there was even a prior suggestion like this.

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-02-23 21:50:30