Auto-Incrementing compile-time constant?

Started by
11 comments, last by Shannon Barber 21 years, 8 months ago
Every now & then I have sections of code that look something like this: static const tag_t TAG_CREATE = 1; static const tag_t TAG_RELEASE = 2; static const tag_t TAG_SEAL = 3; ... Where I declare & define a bunch of magic constants. Being the lazt organizational freak that I am, every now&then I rearrange the order to better group the tag by thier (eventual) functionality. And I hate renumbering them, and inevitably a number is duplicated on accident somewhere. Anyone ever see an idiom where a value automatically increments everytime it''s reference at compile time? macro-magic, meta-template, or something else? Magmai Kai Holmlor "Oh, like you''ve never written buggy code" - Lee [Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ] [Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
enum just not going to cut it?

  enum {   TAG_CREATE = 1,   TAG_RELEASE,   TAG_SEAL,};  


?
Sounds like you are looking for the predefined macro
__COUNTER__    

It's a Visual C++ 7-specific extension, AFAIK. I'd love to see a portable construct, but I'm not sure that's even possible given different translation units etc...


[edited by - spock on August 14, 2002 4:19:49 PM]
Something like this pops to mind:

int _grefcount=0;#define _COUNTER_ (_grefcount+=1) 


Then you use _COUNTER_ all over the place. That wouldn''t evaluate at compile-time, but it works And it can increment, demcrement, whatever.

** "Microsoft OLE DB Provider for SQL Server error ''80040e31''" x10 **
Can''t you generate a file containing the text "const int blah = 1", use some sort of prebuild step to increment it, and simply #include it?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
isn''t there an #undefine or something? Maybe you could do that, expand counter1 to counter2+1 and then turn counter2 into counter1 and so forth?
These techniques all sound suspiciously like enum simulation.
Thanks, can''t believe I didn''t think of enum.

Now, if only there was a way to do it across tranlation units.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Thanks, can''t believe I didn''t think of enum.

Now, if only there was a way to do it across translation units.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
include the header?

This topic is closed to new replies.

Advertisement