mercredi 4 mars 2015

What is the actual purpose of std::type_info::name()?

Today a colleague of mine came and asked me the question as mentioned in the title.

He's currently trying to reduce the binaries footprint of a codebase, that is also used on small targets (like Cortex M3 and alike). Apparently they have decided to compile with RTTI switched on (GCC actually), to support proper exception handling.


Well, his major complaint was why std::type_info::name() is actually needed at all for support of RTTI, and asked, if I know a way to just suppress generation of the string literals needed to support this, or at least to shorten them.



std::type_info::name


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.



A ,- however compiler internal -, implementation of e.g. the dynamic_cast<> operator would not use this information, but rather something like a hash-tag for type determination (similar for catch() blocks with exception handling).

I think the latter is clearly expressed by the current standard definitions for



  1. std::type_info::hash_code

  2. std::type_index


I had to agree, that I also don't really see a point of using std::type_info::name(), other than for debugging (logging) purposes. I wasn't a 100% sure that exception handling will work just without RTTI with current versions of GCC (I think they're using 4.9.1), so I hesitated to recommend simply switching off RTTI.

Also it's the case that dynamic_casts<> are used in their code base, but for these, I just recommended not to use it in favor of static_cast (they don't really have something like plugins, or need for runtime type detection other than assertions).


TL;DR;

So the question stays:

Are there real life, production code level use cases for std::type_info::name() other than logging?


Aucun commentaire:

Enregistrer un commentaire