Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Shannon Barber

Auto-Incrementing compile-time constant?

This topic is 5788 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)]

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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 **

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
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)]

Share this post


Link to post
Share on other sites
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)]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
include the header?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!