C ++ enumeration by step

I am trying to write the enum equivalent in C ++, which will execute in steps of eight instead of one, for example

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

There will be many entries, new ones will be added at intervals, and for clarity, they really want to be recorded in a different order than the input order, so it would be nice not to update all numbers manually. I tried to cheat using preprocessor macros, but still no dice. Is there a way to do this that I am missing?

+4
source share
9 answers
 #define NEXT_ENUM_MEMBER(NAME) \ NAME##_dummy, \ NAME = NAME##_dummy - 1 + 8 enum MyEnum { Foo, NEXT_ENUM_MEMBER(Bar), NEXT_ENUM_MEMBER(Baz), ... }; 
+25
source

I prefer something like this:

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

It is not automated, but adding a new enumeration constant does not require much thought.

+8
source

I'm not quite sure what you are asking, but this approach will automatically generate the values ​​in step 8 and make it relatively easy to insert new values ​​in the middle of the enumeration and update all of the following values ​​to accommodate the change:

  enum
   {
      foo
      bar = foo + 8,
      baz = bar + 8
   }

After editing, to add a "new value", you must:

  enum
   {
      foo
      bar = foo + 8,
      newvalue = bar + 8,
      baz = newvalue + 8
   }

You can also use the Step constant so that you can (a) change your mind about the step later and (b) stop any accidental addition of the wrong step:

  const int EnumStep = 8;

   enum
   {
      foo
      bar = foo + EnumStep,
      baz = bar + EnumStep 
   }
+4
source

You can do the following:

 #define STEP 8 #define ADD_ENUM(X, M) X = STEP * (M) enum { ADD_ENUM(Foo, 0), ADD_ENUM(Bar, 1), ADD_ENUM(Baz, 2), //.. and so on }; 
+3
source

 #include <stdio.h> 

enum{ foo = 0, bar = foo+8, baz = bar+8, };

int main(){ printf("foo: %i bar: %i baz: %i\n", foo, bar, baz ); }

+2
source

Another approach that requires a separete header file for enum , for example entries.h :

 enum { ENTRY(foo), ENTRY(bar), ENTRY(baz) }; 

Then you can use the following

 #define ENTRY(E) _ ## E #include "entries.h" #undef ENTRY #define ENTRY(E) E = _ ## E * 8 #include "entries.h" #undef ENTRY 

for generating

 enum { _foo, _bar, _baz }; enum { foo = _foo * 8, bar = _bar * 8, baz = _baz * 8 }; 
+1
source
 int foo, bar, baz; baz = 8 + bar = 8 + foo; 
0
source
 #define ENUM_ENTRY_PASTE( x ) x #define ENUM_ENTRY_8_SPACING( x ) \ x, \ ENUM_ENTRY_PASTE(x)2, \ ENUM_ENTRY_PASTE(x)3, \ ENUM_ENTRY_PASTE(x)4, \ ENUM_ENTRY_PASTE(x)5, \ ENUM_ENTRY_PASTE(x)6, \ ENUM_ENTRY_PASTE(x)7, \ ENUM_ENTRY_PASTE(x)8 

This is not ideal, as you will have all these additional features, but you will have your own transfers in the right place.

0
source

Another possibility that avoids the use of (MISRA-forbidden) ## operators or the creation of additional enumerations / records:

 #define USE_MYENUM(value) (value*8) enum { foo = 0, bar, baz } MyEnum = bar; CallFunction(USE_MYENUM(MyEnum)); 

Of course, it would be preferable to put USE_MYENUM (or, more likely, MyEnum*8 ) in CallFunction() so that type checking is done.

0
source

All Articles