Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Increment an enumeration object

11 views
Skip to first unread message

Bo Persson

unread,
Oct 8, 2018, 5:19:26 PM10/8/18
to
On 2018-10-08 21:51, Stefan Ram wrote:
> I wrote:
>
> for( i = ::operation::SUM; true; ++i )
>
> where the type of »i« is
>
> enum class operation { NUL, SUM, DIF, MUL, DIV };
>
> . My intention is that »i« has the value of »operation::SUM«,
> then »operation:DIF«, then »operation::MUL« and so on.
>
> The compiler is telling me:
>
> no match for 'operator++' (operand type is 'operation')
>
> . So, I can't enumerate an enum? Why is it called "enum", then?
> Well, what is the usual way to do this then?
>

The usual way is to write an operator++ overload that does what you want.

Note that in the general case, it is not required for the enum values to
be consecutive.

An enum class could also be a bitmask type, like std::filesystem::perms

https://en.cppreference.com/w/cpp/filesystem/perms

How would a possibly built in operator++ enumerate those?


Bo Persson

0 new messages