Search code examples
c++vectormapstypeid

Understanding typeid.name() output for Maps and Vectors in C++


Can someone help to better understand the output of typeid.name() on Maps and Vectors? Please find my program and its output below.

For example: output for std::map<int,int> comes out to TYPE is: [St3mapIiiSt4lessIiESaISt4pairIKiiEEE] where I am able to decipher a little bit i.e. map for std::map ofcourse, Iii for the int,int but what does the rest of the characters mean?

Program:

#include <iostream>
#include <map>
#include <vector>
#include <typeinfo>

using namespace std ;

template <class A>
void showtype (A x)
{
        cout<<"TYPE is: ["<<typeid(x).name()<<"]\n" ;
}

int main()
{
        std::map<int,int> iM ;                  showtype(iM) ;
        std::map<int,std::string> isM ;         showtype(isM) ;
        std::map<std::string,std::string> ssM ; showtype(ssM) ;
        std::vector<std::string> sV ;           showtype(sV) ;
        std::vector<int> iV ;                   showtype(iV) ;
        return 0 ;
}

Output:

%_Host@User> ./typeid.name
TYPE is: [St3mapIiiSt4lessIiESaISt4pairIKiiEEE]
TYPE is: [St3mapIiSsSt4lessIiESaISt4pairIKiSsEEE]
TYPE is: [St3mapISsSsSt4lessISsESaISt4pairIKSsSsEEE]
TYPE is: [St6vectorISsSaISsEE]
TYPE is: [St6vectorIiSaIiEE]
%_Host@User>

Solution

  • Your platform uses the Itanium ABI, and in particular that ABI's name mangling scheme. The detailed rules are in the linked specification.

    Your library implementation chooses to return the (significant part of the) name thus mangled from std::type_info::name. This is a fortunate coincidence not required by the Standard (in fact, the Standard has no concept of linking and name mangling).

    GCC's ABI library contains a function to demangle names in abi::__cxa_demangle (but don't use it on untrusted input, since it is full of vulnerabilities!). There's also a free-standing command line tool to do demangling on Linux, c++filt, and the Linux binutils (nm, objdump, readelf) can demangle names, too. (For the same reason, don't run those on untrusted input.)