0

It would be great to generate a unique ID on the preprocessor to get arround using RTTI to determine a type at runtime. I've come accross the option __ COUNTER__ as a gcc macro define. I was quite happy with that but unfortunately my embedded compiler MDK-ARM does not support it :(.

Is there a way to emulate this counter? This is C++, so template meta progrmaming would be an option. Any ideas?

clambake
  • 772
  • 4
  • 15
  • Would `__LINE__` do the job? It's standard, and unique as long as you're careful not to use it more than once in the same line. – Mike Seymour Apr 24 '14 at 10:30
  • This case is about multiple custom enum generated by users in multiple files in a yet unknown way, I would be quite nervous to `__LINE__` – clambake Apr 24 '14 at 10:51
  • You can get into trouble with `__LINE__` as you can have collisions if you use it on the same line in two different source files. BTW, what functionality do you expect from this typeinfo? Does it has to support checking for inheritance? If all you want is exact type match then you can completely avoid this ID generation stuff. – pasztorpisti Apr 24 '14 at 10:55
  • It's not for inheritance I need to know the type of a enum to identify it. I've already implemented it using typeinfo but I'm not sure if using RTTI on an embedded system is the right way. – clambake Apr 24 '14 at 10:57
  • @clambake Is this some kind of enum class/template to emulate "object-oriented" enums of other languages? Can you post some code examples for typeinfo usage? – pasztorpisti Apr 24 '14 at 11:01
  • Basically you are right! I would love to have a object oriented enum or at least a hashed enum. The typeinfo code hashes over the name of the type and saves this. To find an element the same hash is generated and searched for. I'm also not sure if this a good approach. Note: This part of the code is not timing critical, access will not happen very often. – clambake Apr 24 '14 at 11:15
  • @clambake I've asked for code samples because not all typeinfo related scenarios require you to store the type in the instances, sometimes it is enough to have the typeinfo statically. Do you need typeinfo detection of instances? Usually in case of serialization you are OK with static typeinfo that doesn't increase the size of instances. – pasztorpisti Apr 24 '14 at 11:19
  • Static typeinfo would be okay, basically it is okay to only save the hash generated from typeinfo. Here's another go at a non-RTTI solution: `#define UNIQUE_ID __LINE__ + sizeof(__FILE__)` if we add a salt to that it probably is sufficient. – clambake Apr 24 '14 at 11:31
  • 1
    I've recently asked pretty much the same question: http://stackoverflow.com/questions/23206580/c-construct-that-behaves-like-the-counter-macro – glampert Apr 24 '14 at 12:04

0 Answers0