Edit: highlighting the actual question with more context available if desired.
I want to implement the following method:
template <typename T>
<unspecified> type_identification();
For a generic type T, it must return a (relatively) unique identification that is stable over multiple invocations of the same program and may be used for inter-process communication (so no pointer-based solutions).
Compiler-specific macros/extensions/intrinsics may be used, preferably available for both MSVC and clang.
I have considered std::type_info::hash_code
, or std::type_info::name
but both of those cannot guarantee the same output over multiple invocations of the same program.
Trying to avoid the XY-problem by immediately explaining the problem I am trying to solve.
I have written code to generically store data on file for later use. Each so-called entry in the file is assigned a label by the application code that it must use to access the same entry in a later program invocation. The API basically boils down to:
template <typename T>
void make(const std::string &name, T value);
template <typename T>
T get(const std::string &name);
Note that this is merely example code.
When application code accesses a value through get<T>
, it explicitly specifies the type of the entry so that the implementation may use reinterpret_cast
to give access to the entry as the actual type instead of as a void *
.
Let's assume for the sake of this question that all dangers and pitfalls concerning reinterpret_cast
and persisting data to file have been taken into account.
To avoid nasty crashes because application code has messed up the template argument, I would like to add some type identification to each entry in the file. Basically, when the application code does something like:
make("integer", 5);
auto a = get<std::string>("integer");
I would like to throw an exception indicating the mismatch in actual type and requested type.