2

Why is there no increment operator for enums in C? Even if the corresponding integer values are user-defined it does make perfectly sense to me to iterate with ++ to the next member.

To make it more clear

typedef enum myenum t_myEnum;
enum myenum {
    eMember1,
    eMember2
}
t_myEnum bla = eMember1;

Now I ask what is the reason against bla++ yields eMember2.

User defined assignments to integers values like

enum myenum {
    eMember1 = 0,
    eMember2 = 10
}

shouldn't be an obstacle in my opinion.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
floquet22
  • 323
  • 2
  • 15
  • 2
    I did not understand your question. You want `5++` to be legal or something like that? – Sourav Ghosh Mar 15 '16 at 11:38
  • 3
    No, the question is to have `myEnum x = firstValue; x++; // now x = secondValue`. – Thilo Mar 15 '16 at 11:46
  • 1
    Please clarify your question. As Sourav Ghosh's now deleted answer shows, you haven't made your question clear enough to be unambiguous. – fuz Mar 15 '16 at 11:48
  • edit done, thanks for pointing that out ot me. – floquet22 Mar 15 '16 at 11:53
  • 3
    It's a fair question, but it asks for the rationale of the language designers and standards committee, or, more likely, our guesses or opinions about their rationale. To the extent that answers must indulge in conjecture rather than engage in analysis, this question is off topic here. – John Bollinger Mar 15 '16 at 12:01
  • @John - i do not totally agree. If there is a good reason, this question can be answered, if not - well than i agree with you, we could only guess. i hope there is a reason making my question answerable. – floquet22 Mar 15 '16 at 12:04
  • 3
    The problem with the argument is that you assume ++ would make sense to begin with. It would collide with all other arithmetic in the language, making all arithmetic inconsistent. Why would + on an enum have a special meaning, compared to + on another type? And it would introduce undefined behavior on overflow. – Lundin Mar 15 '16 at 12:08

3 Answers3

3

I am sure it is a drawback of C and C++.

Initially enumerations are considered as sets of integer constants as an alternative for the directive #define. Thus an enumeration in C is a common name for a set of such constants. They were made as simple as possible.:)

There was made a step ahead in C++ and enumerators started to have types of their enumerations. Also you can overload operators ++ for enumerations though I agree with you that it would be better that these operators would be built-in.

For example enumerations could be implemented a similar way as std::initializer_list in C++.

So in my opinion there are only historical reasons for the absence of these operators.

An other drawback is impossibility to get the number of enumerators defined in an enumeration.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

I would guess there is no such operator since the work-around is so trivial:

typedef enum
{
   eMember1 = 0,
   eMember2 = 10
} myenum;

const myenum TABLE [] =
{
  eMember1,
  eMember2
};


for(size_t i=0; i<sizeof(TABLE)/sizeof(*TABLE); i++)
{
  do_something_with(TABLE[i]);
}

Similarly, you could use a const myenum* as iterator.


Another reason why such an operator doesn't exist might be that it doesn't make much sense. Lets pretend there was one, what do you think this code would do?

myenum e = eMember2;
e++;

And what would code like eMember1+1 mean? Value 1 or value 10? Again, there's no consistent logic to it.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • it's not about how to find a Workaround but rather if there is a real reason for the absence of corresponding operators. – floquet22 Mar 15 '16 at 12:01
  • 2
    @floquet22 I rather wonder if there is a real reason for having one, since it doesn't make much sense to me, see updated answer. – Lundin Mar 15 '16 at 12:04
  • I have a similar issue and observing error: Unpermitted operand to operator '++' [MISRA 2012 Rule 10.1, required], Should I not do bla++ ? – kapilddit Sep 10 '19 at 07:15
  • To anyone wondering: you can also iterate over enums without relying on a duplicate array of them, as I show in my answer here: [How can I iterate over an enum?](https://stackoverflow.com/a/69774217/4561887) – Gabriel Staples Feb 03 '22 at 14:43
1

Nothing in the C Standard prevent incrementing variables of enum types.

In your example, the typedef is incorrect because you have a forward reference to an enum type, but the following code demonstrates what I think your question is about:

#include <stdio.h>

typedef enum myenum {
    eMember1,
    eMember2,
} t_myEnum;

int main(void) {
    t_myEnum bla = eMember1;
    printf("bla=%d\n", bla);
    printf("bla++=%d\n", bla++);
    printf("bla=%d\n", bla);
    return 0;
}

Output:

bla=0
bla++=0
bla=1

Of course the value of bla is just incremented by 1, it may correspond to another enumeration value or not, depending on the actual values of the enumeration values in the enum type.

There is no way to enumerate the defined values of a given enum type.

chqrlie
  • 131,814
  • 10
  • 121
  • 189