type_info weird output

in the reference page for type_info( http://www.cplusplus.com/reference/std/typeinfo/type_info/ ) they said that their example program would print out
int is: int
  i is: int
 pi is: int *
*pi is: int


derived is: struct Derived
 *pbase is: struct Base
same type? false


polyderived is: struct Poly_Derived
 *ppolybase is: struct Poly_Derived
same type? true
, but when I ran the very same code, it printed this:
int is: i
  i is: i
 pi is: Pi
*pi is: i

derived is: 7Derived
 *pbase is: 4Base
same type? false

polyderived is: 12Poly_Derived
 *ppolybase is: 12Poly_Derived
same type? true

wich is quite weird!
type_info is implementation defined in what it prints. You are probably just seeing what the compiler decides to store the info as.

Just an FYI:
Possible output:
I think it's highly questionable that any implementation would print "i" as the name of the type of "*pi". I will take a wild guess and say that the op's code is not actually the same.
Last edited on
bump
In truth - my results were exactly as described
by viliml when I used GCC compiler
but visual studio printouts were fine
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
#include <string>
#include <typeinfo>
#include <iostream>

#ifdef __GNUG__

#include <cxxabi.h>

std::string name( const std::type_info& tinfo )
{
    enum { MAX_LEN = 4096 } ;
    char buffer[ MAX_LEN ] ;
    std::size_t size = MAX_LEN ;
    int status ;
    __cxxabiv1::__cxa_demangle( tinfo.name(), buffer, &size, &status ) ;
    return status==0 ? buffer : "__cxa_demangle error" ;
}

#else

std::string name( const std::type_info& tinfo ) { return tinfo.name() ; }

#endif // __GNUG__

int main()
{
    int i = 0 ;
    double array[23] ;

    std::cout << "i => " << name( typeid(i) ) << '\n'
              << "int => " << name( typeid(int) ) << '\n'
              << "array => " << name( typeid(array) ) << '\n'
              << "std::cout => " << name( typeid(std::cout) ) << '\n'
              << "name => " << name( typeid(name) ) << '\n' ;
}

Outrput (GCC):
i => int
int => int
array => double [23]
std::cout => std::ostream
name => std::string (std::type_info const&)

No but, sir. The gcc's output is correct.
I think it's highly questionable that any implementation would print "i" as the name of the type of "*pi".
¿why do you think that? dereferencing a pointer to an int would yell an int.
@JLBorges: thanks, now it works on my comp too.
I feel a headache when I see the codes Hehehe ..
i have study more , i really want become good at it ..
Topic archived. No new replies allowed.