I have a library which persists the state of C++ objects to disk. Since the objects are defined by the end user, I use std::type_info::name()
to get the object name as part of the filename to link the persisted data to corresponding objects. However, according to the C++ reference:
const char* name() const;
Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.
std::type_info::name()
does not guarantee the same value for the same type if application restarted. Worse, it even cannot guarantee distinct names for different types. And std::type_info::hash_code()
is similar.
So, how can I get a distinct name/id of a C++ object that survives application restart, at least on the same operating system and architecture?