Tag Archives: metaprogramming

Compile-time type identification

Yesterday I was working on a class to produce natural-language translations of complex C++ types, and I said that there was no general way to map a fundamental type to a name. Of course, there is:

template<typename T>
class TypeDecryptor {
    static string getName() {
        return typeid(T).name();

Of course, not quite everything in the garden is lovely. This provides an implementation-defined string, which as far as I can see is not even required to be unique for a given type. In point of fact, it’s extremely unlikely an implementation will return a non-unique string since each type has to be mapped to a string for linkage purposes anyway. The problem is that the string isn’t necessarily human-readable.

I think that on reasonable implementations there should be a function available to unmangle this string back into a human-readable type, which means you can potentially make a completely general type decryptor.