curious about typeid().name()

N

Noah Roberts

In reading "Thinking about C++ V2" I started using the TestSuite
described in Ch2. In it there are lines that print out
typeid(*this).name(). In the book it shows up as "TestXXX" but in
actual experience with G++ it prints out "##TestXXX" instead. For
instance, WinHelpersTest gets printed as "14WinHelpersTest".

Is G++ behaving appropriately? No other examples on web of
type_info::name show a return like that; they all show "ClassName"
instead of "##ClassName".
 
V

Victor Bazarov

Noah said:
In reading "Thinking about C++ V2" I started using the TestSuite
described in Ch2. In it there are lines that print out
typeid(*this).name(). In the book it shows up as "TestXXX" but in
actual experience with G++ it prints out "##TestXXX" instead. For
instance, WinHelpersTest gets printed as "14WinHelpersTest".

Is G++ behaving appropriately? No other examples on web of
type_info::name show a return like that; they all show "ClassName"
instead of "##ClassName".

The Standard does not specify how the name should appear. In fact, it
doesn't require anything meaningful at all. It's implementation-defined,
and for all C++ cares, your compiler may just output an empty string for
every type there is.

V
 
A

Andrey Tarasevich

Noah said:
In reading "Thinking about C++ V2" I started using the TestSuite
described in Ch2. In it there are lines that print out
typeid(*this).name(). In the book it shows up as "TestXXX" but in
actual experience with G++ it prints out "##TestXXX" instead. For
instance, WinHelpersTest gets printed as "14WinHelpersTest".

Is G++ behaving appropriately? No other examples on web of
type_info::name show a return like that; they all show "ClassName"
instead of "##ClassName".

The specific form of the return value of 'type_info::name()' is
implementation defined. Strictly speaking, the implementation is not
even required to return anything "meaningful" from 'type_info::name()'.
Formally, it can return a pointer to "Hello World!" string every time
you call it, for all instances of 'type_info'.

Normally, the compiler would return a more or less mangled string, which
at least remotely resembles the actual name of the type. What you
observe in your case is perfectly fine, nothing unusual.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,649
Latest member
MargaretCo

Latest Threads

Top