Archived

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

Shannon Barber

Auto-Incrementing compile-time constant?

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

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
quote:
Original post by Magmai Kai Holmlor
Thanks, can't believe I didn't think of enum.

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


quote:
include the header?

lol another advanced C++ feature.

Seriously, what do you mean by "doing it across translation units"? If you #include the header, it's going to be in every TU...

If you're thinking about multiple enums having different values, I think you can do this:

enum {A, B, C, D};
enum {E = D + 1, F, G, H};

Cédric

[edited by - cedricl on August 15, 2002 3:22:10 PM]

Share this post


Link to post
Share on other sites
No, I mean defining additional unique tags, across translation units (starting to sound like UUIDs).

Each translation unit would magically be aware of where the last one left off, and would continue numbering from there.

For example, whenever somone makes a WM_USER message, they end up using the same number someone else did - I want them to be unique. (Not for WM_USER messages, but for custom message tags).


[edited by - Magmai Kai Holmlor on August 15, 2002 5:47:36 PM]

Share this post


Link to post
Share on other sites
My remark about different translation units were based on the assumption that actual objects (constant or otherwise) needed to be defined based on the values. Sorry if I confused anyone. When values are all that is needed, an enum is certainly a natural and straightforward solution.

When objects are needed, the enum solution gets a bit clumsy because you need to update both the enum and the variable definitions. In such a case, some variation of Zipster's solution will work better. But then the auto-incremented values won't be compile-time constants, which they need to be if you want use them as template parameters or case labels. That's where the __COUNTER__ macro comes in handy, but that will obviously only work in a single compilation unit.

AFAIK you can get auto-incrementing OR compile-time values across translation units, but not both. For something like custom message tags, an enum is likely your best bet.


[edited by - spock on August 15, 2002 6:41:30 PM]

Share this post


Link to post
Share on other sites