Re:Some obscure C features
by cperciva on Thursday January 25, 2018 @08:15PM
(#16236292)
Note that this does not work if you make use of __COUNTER__ anywhere else in the file (directly or indirectly).
[ Parent
| on HN ]
That's true, and in the event that you're actually using __COUNTER__ for something you could build something similar out of #include with a header that increments an internal number.
// COUNTER.h
#if !defined(COUNTER)
#define COUNTER 0
#elif COUNTER == 0
#undef COUNTER
#define COUNTER 1
#elif COUNTER == 1
#undef COUNTER
#define COUNTER 2
...
#end if
And then change each invocation of ELEMENT to have a leading #include "COUNTER". #include "COUNTER.h"
ELEMENT(x)
#include "COUNTER.h"
ELEMENT(y)
Note that if you take this approach then (as far as I know) you're actually writing standard C!