No. There is no portable way to get the name as a string for an arbitrary type across all platforms.
You can potentially write a function template that uses platform-specific code to determine a name of a template parameter, but that is tricky to do, and brittle.
Alternatively, if you only want names for your types, or you are willing to register names for every type that you need a name for, then you can do something like:
some_library::register_type_name<some_type>("some_type");
some_library::register_type_name<some_other_type>("some_other_type");
adding entries to an internal map of typeid
to string. This could be hidden behind a macro like REGISTER_TYPE(x)
, but it would still need to be done for every type.
You can then easily write some_library::lookup_type_name<some_type>()
which searches the registered types and returns the string. You could also write an overload especially for S
which looked up the name of its template parameters too.