Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange output of std::typeid::name()

Tags:

c++

typeinfo

I used typeid to get the type names of the std::vector::size_type and a zero sized class A with the following code (cppreference):

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

using namespace std;

class A {};

int main()
{
    vector<int> v(10); 

    vector<int>::size_type s = v.size(); 

    A a; 

    cout << typeid(s).name() << endl;
    cout << typeid(a).name() << endl;

};

And I got this as output:

m
1A

I guess that "1" before "A" is a result of the Empty Base Class Optimization, but what does "m" stand for and is this normal?

I am using the following gcc version: g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3

like image 887
tmaric Avatar asked May 06 '13 10:05

tmaric


People also ask

What does Typeid name return C++?

The typeid operator returns an lvalue of type const std::type_info that represents the type of expression expr. You must include the standard template library header <typeinfo> to use the typeid operator.

What is M in Typeid?

@tomislav-maric the name "m" stands for unsigned long , which happens to be typedef for std::size_t , which happens to be the typedef for std::vector<>::size_type . Typedefs do not create new types.

What is std :: type_info?

std::type_info The class type_info holds implementation-specific information about a type, including the name of the type and means to compare two types for equality or collating order.

What is typeof in C++?

typeof is a GNU extension, and gives you the type of any expression at compile time. This can be useful, for instance, in declaring temporary variables in macros that may be used on multiple types. In C++, you would usually use templates instead.


1 Answers

G++ uses implementation-defined naming for the types, but it also offers the utility c++filt to make them human-readable:

$ ./test | c++filt -t
unsigned long
A
like image 170
Cubbi Avatar answered Oct 19 '22 11:10

Cubbi