1

I have a series of #defines that I want assigned consecutive numbers at compile time so I can refer to them later and know their order. Currently, I'm assigning a number to each #define in order, but, if I change the order, I have to renumber the #defines.

#define THING_A     1 // I'm doing this now...
#define THING_F     2
#define THING_C     3
#define THING_B     4

Not shown: these #define are between array elements and refer to the element order. I often need to rearrange the element order. If I reorder them, as below, I want their numbers to change accordingly without me having to go through and edit the numbers like this:

#define THING_C     1
#define THING_A     2
#define THING_B     3
#define THING_F     4

What can I replace the numbers with (the same for every #define) that will result in consecutive assignments? I can't use "__COUNTER_" like this...

#define THING_B     (__COUNTER__)

... because every time I use the defined value later, it gets larger. What is this "(something)"?

#define THING_A     (something)
#define THING_B     (something)
#define THING_C     (something)
#define THING_D     (something)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jeff
  • 2,659
  • 1
  • 22
  • 41
  • 4
    What about an enumeration instead of #define? The values of the enum will get values in order. – rmaddy Dec 16 '18 at 02:36
  • The essence of what you're trying to do here is to have macros expand according to the order you define the macros in. That, fundamentally, is impossible; expansion of a macro invocation is entirely a function of _which_ macros are defined at the time of invocation, and _what_ they are defined as. The order in which they were defined plays no role whatsoever. Now you could chain the definitions, which defeats the point; auto-increments like counter's won't work because they're invocation features not definition ones. The best thing is to just use enum's like @rmaddy suggests. – H Walters Dec 16 '18 at 06:42
  • 1
    Thank you. I have found a way to use an enum as you both suggest, a la https://stackoverflow.com/a/3035640/236415 . – Jeff Dec 18 '18 at 05:32

1 Answers1

0

See this answer https://stackoverflow.com/a/3035640/236415 for how to use an enum as suggested by the commenters.

Jeff
  • 2,659
  • 1
  • 22
  • 41