Possible Duplicate:
typeid() returns extra characters in g++
I learned from cplusplus.com that typeid function can get variable’s type infomation in C++. But the result is a bit strange in my machine.
For example:
#include <iostream>
#include <typeinfo>
#include <vector>
using namespace std;
class Test
{
};
int main()
{
vector<int> v;
vector<vector<int> > v2;
Test t;
cout<<"typeid for class Test: "<<typeid(t).name()<<endl;
cout<<"typeid for vector<int>: "<<typeid(v).name()<<endl;
cout<<"typeid for vector<vector<<int> >: "<<typeid(v2).name()<<endl;
return 0;
}
After running this code, the output in my machine is:
typeid for class Test: 4Test
typeid for vector<int>: St6vectorIiSaIiEE
typeid for vector<vector<<int> >: St6vectorIS_IiSaIiEESaIS1_EE
I’m quite confused with the “4” before the class “Test” and the strange letters around the vector. Can anybody give me some explanations? Thank you!
My operating system is Fefora 17, g++ (GCC) 4.7.0 20120507 (Red Hat 4.7.0-5)
typeid function can get variable's type infomation in C++It’s true, but when u call the
type_info::name()function you get an unique name for each different type, but the standard does not guarantee that the name is somewhat meaningful.And it is also implementation-dependent.
What you get in practice (at least for most/all implementation) is the mangled name of that type.
In short, you can use
type_info::name()only for comparison, or for debugging (and you’ll have to demangle the name by yourself to get something meaningful)EDIT
Since you use gcc, you may want to check out this page: http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html