typeid().name()

When we do a typeid().name() we get the name of the class which the reference or pointer belongs...but something like an id appears....Here I leave my example..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <typeinfo>
#include <iostream>
using namespace std;

class Storable{
public:
	
	virtual void read() =  0;
	virtual void write() = 0;
	virtual ~Storable() {
		
	}
};

class Component: public Storable{
	 void write(){};
	 void read(){};
};

class Transmitter: public virtual Component{
public:
	void read(){
		
	}
	void write(){
		
	}
	~Transmitter(){
	}
	Transmitter(int a):T(a){}
protected:
	int T;
};

class Receiver: public virtual Component{
public:
	void read(){
		
	}
	void write(){
		
	}
	~Receiver(){
  }
	Receiver(int b):R(b){}
protected:
	int R;

};

class Radio: public Transmitter, public Receiver{
public:
	void read(){
		
	}
	void write(){
	
		
	}
	~Radio(){
	}
	Radio(int a,int b):Transmitter(a),Receiver(b){}
	
};

void f(Storable& ref,Storable* ptr){
	cout<<typeid(ref).name()<<endl;
	cout<<typeid(*ptr).name()<<endl;
	cout<<typeid(ptr).name()<<endl;
}

int main(){
	Transmitter objTransmitter(3);
	Storable* ptrStorable = &objTransmitter;
	
	f(objTransmitter,ptrStorable);
	return 0;
	
	
}




the output is:
11Transmitter
11Transmitter
P8Storable

the P word make sense, it means pointer...the names are fine...but the number....It seems like is a classification according to the hierarchy..but how is that?
std::type_info::name():
Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given, in particular, the returned string can be identical for several types and change between invocations of the same program.
http://en.cppreference.com/w/cpp/types/type_info/name


The GNU and LLVM compilers return a mangled name. It can be demangled into a human readable form by filtering it with c++filt, or with the implementation-provided __cxxabiv1::__cxa_demangle() function.

The Microsoft compiler returns a human readable string; no post-processing is required.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <typeinfo>
#include <string>
#include <memory>
#include <cstring>

#ifdef __GNUG__ // gnu C++ compiler

#include <cxxabi.h>

std::string demangle( const char* mangled_name ) {

    std::size_t len = 0 ;
    int status = 0 ;
    std::unique_ptr< char, decltype(&std::free) > ptr(
                __cxxabiv1::__cxa_demangle( mangled_name, nullptr, &len, &status ), &std::free ) ;
    return ptr.get() ;
}

#else

std::string demangle( const char* name ) { return name ; }

#endif // _GNUG_

namespace one {

    class Storable { public: virtual ~Storable() = default ; };
    class Component : public Storable {};
    class Transmitter: public virtual Component {};
    class Receiver: public virtual Component {};

    namespace two {
        class Radio: public Transmitter, public Receiver {};
    }
}

int main()
{
    one::two::Radio radio ;
    std::cout << typeid(radio).name() << '\n'
              << demangle( typeid(radio).name() ) << "\n\n" ;

    one::Transmitter& t = radio ;
    std::cout << typeid(t).name() << '\n'
              << demangle( typeid(t).name() ) << "\n\n" ;

    const one::Receiver* r = std::addressof(radio) ;
    std::cout << typeid(r).name() << '\n'
              << demangle( typeid(r).name() ) << "\n\n" ;

    std::cout << typeid( *r ).name() << '\n'
              << demangle( typeid(*r).name() ) << "\n\n" ;

    std::cout << typeid(std::cin).name() << '\n'
              << demangle( typeid(std::cin).name() ) << "\n\n" ;

    std::cout << typeid(std::strcpy).name() << '\n'
              << demangle( typeid(std::strcpy).name() ) << "\n\n" ;
}

http://coliru.stacked-crooked.com/a/57f2b804711a7251
http://rextester.com/IPET45680
as the documentation on the function you're calling says here: http://en.cppreference.com/w/cpp/types/type_info/name

With compilers such as gcc and clang, the returned string can be piped through c++filt -t to be converted to human-readable form.


so do that:

$ echo 11Transmitter | c++filt -t
Transmitter

$ echo P8Storable | c++filt -t
Storable*
I can't compile because the compiler says that free is not a member of std....in general project setting my compiler is gnu c++....

And I'm not familiar with smart pointers...I don't know well what that's doing...as a general idea I get that It's converting a mangled name in a human readable word by activating a macro which cointain demangle....
> the compiler says that free is not a member of std
> And I'm not familiar with smart pointers...I don't know well what that's doing..
> It's converting a mangled name in a human readable word by activating a macro

__cxa_demangle() is a function in the namespace __cxxabiv1

Try this no-smart-pointer version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifdef __GNUG__ // gnu C++ compiler

#include <cxxabi.h>
#include <stdlib.h>

std::string demangle( const char* mangled_name ) {

    std::string result ;
    std::size_t len = 0 ;
    int status = 0 ;
    char* ptr = __cxxabiv1::__cxa_demangle( mangled_name, nullptr, &len, &status ) ;

    if( status == 0 ) result = ptr ; // hope that this won't throw
    else result = "demangle error" ;

    ::free(ptr) ;
    return result ;
}

#else

std::string demangle( const char* name ) { return name ; }

#endif // _GNUG_ 
ok, this is more readable for me thanks!!
closed account (E0p9LyTq)
@Winsu,

here's the output of your source compiled using VS C++ 2015 Community:

class Transmitter
class Transmitter
class Storable *
how can it be?? depend of the compiler I can get other features on the same instructions??
closed account (E0p9LyTq)
GCC/GNU/TVMM implement the C++ requirements one way, VS C++ implements it in a different manner.

Same idea, getting distinct names for different type ids, just done differently by each implementation.

Welcome to the compiler "wars." ;)
ehheehhe, nice introduction!!!,but the point is, I can have different result about the warnings and errors but the application will be the same for each compiler with the same code...of course respecting what each one can assume....
Topic archived. No new replies allowed.