11

I want to generate unique numbers for each class in my header, primes in my case primes but let's say this should only be consecutive numbers i.e. 1,2,3,4,etc.

Of course I can hardcode these:

struct A { enum { ID = 1; }; };
struct B { enum { ID = 2; }; };
struct C { enum { ID = 3; }; };
struct D { enum { ID = 4; }; };

This is very error-prone since in reality the classes are not that small and if I add a new class in the middle I have to change all the following numbers if I don't want to completely loose the overview of the IDs.

I wish I could do the following:

struct A { enum { ID = get_next_int(); }; };
struct B { enum { ID = get_next_int(); }; };
struct C { enum { ID = get_next_int(); }; };
struct D { enum { ID = get_next_int(); }; };

But since constexpr functions calls can't have side effects afaik, this is impossible. I think using macros such a result is impossible too.

I would also be lucky with something like that:

struct A_id_holder : some_base_counter {};
struct A { enum { ID = A_id_holder::ID; }; };

struct B_id_holder : some_base_counter {};
struct B { enum { ID = B_id_holder::ID; }; };

struct C_id_holder : some_base_counter {};
struct C { enum { ID = C_id_holder::ID; }; };

struct D_id_holder : some_base_counter {};
struct D { enum { ID = D_id_holder::ID; }; };

But honestly, I have no idea how to implement that.

Can I achieve my goal and if yes, how?

helami
  • 2,099
  • 2
  • 14
  • 17
  • What does this have to do with TMP? Is that an avenue you have explored, or are you just hoping a TMP wizard will come along with an answer out of the blue? – jpm Mar 30 '12 at 19:51
  • Yes, since this can't be done with macros, TMP is my last hope (or how to initialize an ID otherwise?). I would also be glad about other possibilities. I edited my post to be more general. – helami Mar 30 '12 at 19:57
  • Possible dupe, or answer: http://stackoverflow.com/questions/6166337/does-c-support-compile-time-counters – Nate Kohl Mar 30 '12 at 20:14
  • For C: http://stackoverflow.com/questions/1132751/how-can-i-generate-unique-values-in-the-c-preprocessor – Ciro Santilli OurBigBook.com Jun 21 '15 at 11:39

4 Answers4

4

I think that Boost preprocessor library can do that for you, maybe you should read that: How can I generate unique values in the C preprocessor?

There is an alternative depending on the compiler that you are using, gcc and msvc have a ___COUNTER___ macro that allows sequential number: http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html#Common-Predefined-Macros

Community
  • 1
  • 1
Mesop
  • 5,187
  • 4
  • 25
  • 42
3

Most people do this with the __COUNTER__ macro. But that's nonstandard, and there's only one for the whole program.

Here is a C++ hack I came up with using templates and overloading which is standard-compliant and supports multiple counters.

Community
  • 1
  • 1
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
1

If you use gcc, you can use the __COUNTER__ macro.

Idelic
  • 14,976
  • 5
  • 35
  • 40
0

One way might be to hard-code a placeholder wherever you want a unique number, and then write a small utility to pre-process the files, perhaps keeping the last-used number in a file so it will persist across invocations.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150