Creating serializeable unique compile-time identifiers for arbitrary UDT's.

Posted by Endiannes on Stack Overflow See other posts from Stack Overflow or by Endiannes
Published on 2011-01-09T18:50:22Z Indexed on 2011/01/09 18:53 UTC
Read the original article Hit count: 194

Filed under:
|
|

I would like a generic way to create unique compile-time identifiers for any C++ user defined types.
for example:

unique_id<my_type>::value == 0 // true
unique_id<other_type>::value == 1 // true  

I've managed to implement something like this using preprocessor meta programming, the problem is, serialization is not consistent. For instance if the class template unique_id is instantiated with other_type first, then any serialization in previous revisions of my program will be invalidated.

I've searched for solutions to this problem, and found several ways to implement this with non-consistent serialization if the unique values are compile-time constants. If RTTI or similar methods, like boost::sp_typeinfo are used, then the unique values are obviously not compile-time constants and extra overhead is present. An ad-hoc solution to this problem would be, instantiating all of the unique_id's in a separate header in the correct order, but this causes additional maintenance and boilerplate code, which is not different than using an enum unique_id{my_type, other_type};.

A good solution to this problem would be using user-defined literals, unfortunately, as far as I know, no compiler supports them at this moment. The syntax would be 'my_type'_id; 'other_type'_id; with udl's.

I'm hoping somebody knows a trick that allows implementing serialize-able unique identifiers in C++ with the current standard (C++03/C++0x), I would be happy if it works with the latest stable MSVC and GNU-G++ compilers, although I expect if there is a solution, it's not portable.

© Stack Overflow or respective owner

Related posts about c++

Related posts about preprocessor