Is there a good template for creating a unique identifier based on a type?

I have a template that creates a unique identifier for each type that is created. Here is a simplified version of the template:

template <typename T> class arType { static const arType Id; // this will be unique for every instantiation of arType<>. } // Address of Id is used for identification. #define PA_TYPE_TAG(T) (&arType<T >::Id) 

This works when you have an executable consisting entirely of static libraries. Unfortunately, we go to the executable from the dll. Each DLL can potentially have its own copy of the Id for the type.

One obvious solution is to explicitly instantiate all arType instances. Unfortunately, this is cumbersome, and I would like to ask if anyone can suggest a better solution?

+1
c ++ metaprogramming
source share
3 answers

Returns an std :: type_info object from a function for each object and uses the == operator for the result. You can sort them using the before () function, which returns the sort order.

It is specifically designed to do what you want. You can wrap it with an opaque type "id" with the operator <if you want to hide how it works under it.

http://www.cplusplus.com/reference/std/typeinfo/type_info/

+1
source share

Yes, and it's easier than you think:

 template<...> class withAnID { static void idFunction(){}; } 

Using:

 &withAnID<...>::idFunction; 

Since each instance of the template gets its own assembly, each function has a unique (sortable!) Address, and you can simply use them to identify them.

0
source share

Well, I think the only way is to program the method processing identifier, which takes into account the problem with the DLL :)

I mean, if you can calculate the global type identifier as "DLL Id" + "DLL local type DLL", then you have what you want. I think this may be feasible if you are managing the DLL loading part or if the OS gives you callbacks to manage this. If you can set a unique identifier in a DLL-related object, you will do :-)

I am not an expert in managing Windows DLLs, but I remember that there is something like the "on_dll_load" function that can do the trick. Any specialist in managing a dll?

Only my 2 cents ...

0
source share

All Articles