C++ Logo

std-proposals

Advanced search

[std-proposals] Enum class extensions

From: Dragan Grbic <dgrbic_at_[hidden]>
Date: Wed, 23 Feb 2022 11:12:15 +0100
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"].

Received on 2022-02-23 10:12:27