value of typeid().name()

I guess i could understand c as char, d as double, etc. etc., But what is PKc?

Can you use the response of typeid().name() as a flag?

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
#include <iostream>
#include <string>
#include <typeinfo>


template<class T>
void print(T var, std::string end="\n"){
    std::cout << var << " is of type " << typeid(var).name() << end;
}

template<class T>
class App{
    public:
        T var;
        App(T arg) :var(arg){}
        
};

class Test{};




int main(){
    print("string");
    print('b');
    print(1.1);
    print(1);
    
    App<std::string> obj("obj_str");
    print(obj.var);
    
    App<int> obj2(1);
    print(obj2.var);
    
    Test o;
    App<Test> obj3(o);

    std::cout << "o is of type " << typeid(o).name() << std::endl;
}


output being:
1
2
3
4
5
6
7
string is of type PKc
b is of type c
1.1 is of type d
1 is of type i
obj_str is of type Ss
1 is of type i
o is of type 4Test


Why would obj_str be of type Ss, while string is of type PKc?
Last edited on
I assume the output is compiler-dependent. My compiler gives this output:
string is of type const char *
b is of type char
1.1 is of type double
1 is of type int
obj_str is of type std::basic_string<char,std::char_traits<char>,std::allocator<char> >
1 is of type int
o is of type Test
Last edited on
oh thats interesting. I guess that answers my question whether or not you can use it as a flag

What version of gcc are you currently using?

Mine is:
4.8.1
1
2
3
4
5
6
7
8
9
metulburr@ubuntu:~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.1-2ubuntu1~13.04' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~13.04) 
metulburr@ubuntu:~$ 
I tried it with Embarcadero C++ Builder. I do use gcc as well and most of the time the two compilers behave in the same way. I wonder what output the Microsoft compiler gives here.
Last edited on
Why would obj_str be of type Ss, while string is of type PKc?

Pass your output through c++filt -t and you will see your type names in human-readable format.
Or call __cxxabiv1::__cxa_demangle() yourself.

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
#include <string>
#include <typeinfo>

#ifdef __GNUG__

    #include <cxxabi.h>
    #include <cstdlib>
    #include <memory>

    template< typename T > std::string type_name()
    {
        int status ;
        std::unique_ptr< char[], decltype(&std::free) > buffer(
            __cxxabiv1::__cxa_demangle( typeid(T).name(), nullptr, 0, &status ), &std::free ) ;
        return status==0 ? buffer.get() : "__cxa_demangle error" ;
    }

#else // !defined __GNUG__

    template< typename T > std::string type_name() { return typeid(T).name() ; }

#endif //__GNUG__

template< typename T > std::string type_name( const T& ) { return type_name<T>() ; }


#define print_type_name(var) ( std::cout << #var << " is of type " << type_name(var) << "\n\n" )

template<class T> class App{
    public:
        T var;
        App(T arg) :var(arg){}

};

class Test{};

#include <iostream>
#include <vector>
#include <cstring>

int main(){
    print_type_name("string");
    print_type_name('b');
    print_type_name(1.1);
    print_type_name(1);

    App<std::string> obj("obj_str");
    print_type_name(obj.var);

    App<int> obj2(1);
    print_type_name(obj2.var);

    Test o;
    App<Test> obj3(o);
    print_type_name(obj3.var);
    print_type_name(obj3);

    std::vector<int> vector ;
    print_type_name(vector);
    print_type_name(vector.rbegin());

    print_type_name(std::strcat);
    print_type_name( &std::strcat );
}

http://ideone.com/3MbeCr
Last edited on
Topic archived. No new replies allowed.