Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ enum by step

Tags:

c++

c

I'm trying to write the equivalent of an enum in C++ going in steps of eight instead of one, like

enum
{
    foo,
    bar = 8,
    baz = 16,
};

There will be a lot of entries, new ones will be added at intervals, and for clarity they really want to be written in an order other than order of entry, so it would be nice not to have to keep updating all the numbers by hand. I've tried mucking around with macro preprocessor tricks, but no dice so far. Is there a way to do this that I'm overlooking?

like image 469
rwallace Avatar asked Nov 26 '22 22:11

rwallace


2 Answers

#define NEXT_ENUM_MEMBER(NAME) \
    NAME##_dummy, \
    NAME = NAME##_dummy - 1 + 8

enum MyEnum {
   Foo,
   NEXT_ENUM_MEMBER(Bar),
   NEXT_ENUM_MEMBER(Baz),
   ...
};
like image 98
Pavel Minaev Avatar answered Nov 29 '22 13:11

Pavel Minaev


I prefer something like this:

enum {
    foo = (0 << 3),
    bar = (1 << 3),
    baz = (2 << 3),
};

It's not automated, but it doesn't require much thinking when adding a new enum constant.

like image 22
Lee Baldwin Avatar answered Nov 29 '22 13:11

Lee Baldwin