Ежедневный бит (e) C++ # 201, Сопоставление std:: type_info с именами типов, удобочитаемыми для человека, с использованием C++ 11 std:: type_index и boost:: type_index.

Объект std::type_info, возвращаемый оператором typeid, не предоставляет доступ к удобочитаемому имени типа.

Мы можем смягчить эту проблему вручную, полагаясь на C++11 std::type_index и вручную отслеживая сопоставление между std::type_info и любой необходимой информацией.

#include <unordered_map>
#include <string>
#include <typeindex>
#include <iostream>

struct A { virtual void fun() {} };
struct B : A {};

std::unordered_map<std::type_index, std::string> names;

void inspector(A& a) {
    std::cout << names[typeid(a)] << "\n";
}


// Fill our custom mapping
names[typeid(A)] = "Base";
names[typeid(B)] = "Derived";
names[typeid(int)] = "int";
names[typeid(double)] = "double";

int x = 20;
double y = 2.4;
std::cout << names[typeid(x)] << "\n";
// prints: "int"
std::cout << names[typeid(y)] << "\n";
// prints: "double"

A a;
B b;
inspector(a);
// prints: "Base"
inspector(b);
// prints: "Derived"

Откройте пример в Compiler Explorer.

В качестве альтернативы мы можем положиться на библиотеку boost::type_index, которая предоставляет дополнительные возможности.

#include <boost/type_index.hpp>
#include <unordered_map>
#include <vector>
#include <string>
#include <iostream>

namespace bti = boost::typeindex;

struct A { virtual void fun() {} };
struct B : A {};

void inspector(A& a) {
    std::cout << bti::type_id_runtime(a).pretty_name() << "\n";
}


// Compile time type information:
int x = 20;
double y = 2.4;
std::cout << bti::type_id<decltype(x)>().pretty_name() << "\n";
// prints: "int"
std::cout << bti::type_id<decltype(y)>().pretty_name() << "\n";
// prints: "double"

// Runtime type information:
A a;
B b;
inspector(a);
// prints: "A"
inspector(b);
// prints: "B"

// Also supports cv qualifiers (typeid doesn't)
std::cout << bti::type_id_with_cvr<const int>().pretty_name() << "\n";
// prints: "int const"

// We also get demangling for complex nested types
using nested = std::unordered_map<A,std::vector<std::string>>;
std::cout << bti::type_id<nested>().pretty_name();
// prints (GCC): "std::unordered_map<A, std::vector<
//                  std::__cxx11::basic_string<char...

Откройте пример в Compiler Explorer.