Create serializable unique compile-time identifiers for arbitrary UDTs

I need a general way to create unique compile-time identifiers for any custom C ++ types.
eg:

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

I managed to implement something like this using preprocessor metaprogramming, the problem is that serialization is incompatible. For example, if you first unique_idcreate an instance template using other_type, then any serialization in previous versions of my program will be invalidated.

I searched for solutions to this problem and found several ways to implement this with incompatible serialization if the unique values ​​are compile time constants. If RTTI or similar methods are used, such as boost::sp_typeinfo, then the unique values ​​are obviously not compile-time constants and additional overhead is present. A special solution to this problem would be to create a unique_id instance in a separate header in the correct order, but this will cause an additional service code and template, which is no different from use enum unique_id{my_type, other_type};.

A good solution to this problem would be to use custom literals, unfortunately, as far as I know, no compiler supports them at the moment. The syntax will be 'my_type'_id; 'other_type'_id;using udl.

, - , ++ (++ 03/++ 0x), , MSVC GNU-g++, , , .

, mpl::set , mpl::vector , , -/ , .

+3
3

, @script_name(args) ++ script, ./script_name.pl args ./script_name.py args.

++, , , @sha1(my_type), , .

, . , , ; , , , , , , .

+2

- .

: ? , .

, , typeid , .

// static detection
template <typename T>
size_t unique_id()
{
  static size_t const id = some_hash(typeid(T)); // or boost::sp_typeinfo
  return id;
}

// dynamic detection
template <typename T>
size_t unique_id(T const& t)
{
  return some_hash(typeid(t)); // no memoization possible
}

. , , main

unique_id<some_type>::value, , , ( ) .

, : .


, , :

  • , ""
  • ( )
  • β†’ .

. . Google Proto Buffer:

  • β†’ ( )
  • / (++, Java, Python)
+1

, , , . MSVC __COUNTER__ , .

0

Source: https://habr.com/ru/post/1784402/


All Articles