I have two versions of a c++ compiler installed on my computer. One of them recognizes the __COUNTER__ macro and the other does not. After doing some research to make the program compile in both I have yet to come across the Macro definition for __COUNTER__. Is this some special Macro done by the compiler or can I copy the definition for __COUNTER__ into my source code, if I can copy it what is the code I need.
- 458
- 1
- 4
- 14
-
1Are you using to create unique identifiers? Example: `#define GLUE(var) (var ## __COUNTER__)`. – Fiddling Bits Dec 25 '13 at 01:46
-
I am using it in an obfuscation challenge to do preprocessor loops. @FiddlingBits – Gibby Dec 25 '13 at 01:48
-
Boost.PP has one of these. – chris Dec 25 '13 at 01:49
-
What compiler was it, by the way? – einpoklum Dec 16 '15 at 21:10
2 Answers
__COUNTER__ is a built-in in several common compilers. It is not possible to define manually. If you're stuck with a compiler that doesn't support it, your best option might be to run your code through a preprocessor that does support it before feeding it into the compiler.
- 122,243
- 18
- 242
- 228
It's a special macro which has been introduced by Visual Studio and I think is now supported by GCC too.
It basically provides a unique counter over integral numbers which can be used to generate unique identifiers.
From GCC release notes:
A new predefined macro
__COUNTER__has been added. It expands to sequential integral values starting from 0. In conjunction with the##operator, this provides a convenient means to generate unique identifiers.
If you don't have it available to a compiler you can easily mimic the behavior with a static variable. But I'm not sure what you are compiling so I'm not sure how this counter is used in the code you have available.
- 128,551
- 28
- 227
- 331
-
1You can't increment a variable with the preprocessor... how would using a variable work? – Fiddling Bits Dec 25 '13 at 01:50
-
@FiddlingBits I wrote a little hack once that has a `INIT` macro that set `COUNTER=0;`, and another macro which included `COUNTER++;`. It is not fully functional, but it can server pretty well, despite requiring a global variable. – dmckee --- ex-moderator kitten Dec 25 '13 at 02:15
-
1@FiddlingBits: it depends how the actual `__COUNTER__` macro is used inside the code. You could be able to mimic the behavior or not according to how the generated number is used. – Jack Dec 25 '13 at 02:16
-
-
@FiddlingBits, Take a look at [Boost's](http://www.boost.org/doc/libs/1_55_0/libs/preprocessor/doc/ref/counter.html). – chris Dec 25 '13 at 02:26
-
-
@FiddlingBits, Check your installation. It's definitely open source and it's actually interesting how high it can go. – chris Dec 25 '13 at 02:31