Auto-Incrementing compile-time constant?

Started by
11 comments, last by Shannon Barber 21 years, 8 months ago
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]
Advertisement
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]
- 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
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]

This topic is closed to new replies.

Advertisement