Discussion:
How to use std::typeid ?
(too old to reply)
David List
2003-08-08 15:17:28 UTC
Permalink
I am trying to use some code, that should yield the type name of a
given variable. There is an example in Stroustrup, section 15.4.4,
page 415.

I get only the first character of the type name, however. Can anyone
pinpoint my problem?

An example of my usage is:

----------main.cpp----------
#include <iostream>
#include <typeinfo>
using namespace std;

int main()
{
int a = 0;
cout << "typename of a is " << typeid(a).name() << endl;
}
----------main.cpp----------

This program produces a line of text, that says:
typename of a is i
--
Med venlig hilsen / Best regards
David List
Jim Fischer
2003-08-08 16:21:30 UTC
Permalink
Post by David List
I am trying to use some code, that should yield the type name of a
given variable. There is an example in Stroustrup, section 15.4.4,
page 415.
I get only the first character of the type name, however. Can anyone
pinpoint my problem?
----------main.cpp----------
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int a = 0;
cout << "typename of a is " << typeid(a).name() << endl;
}
----------main.cpp----------
typename of a is i
The string value returned from the .name() method is implementation
defined. In other words, there is no requirement that

typeid(int).name()

return the string "int". So there is nothing "wrong" with g++ 3.3
returning the string "i" instead of "int" here.


FWIW, you can use the 'c++filt(1)' program to "demangle" the string
value that's returned from the typeid .name() method:

# typeid(int).name() --> i
$ c++filt --format=gnu-v3 i
int

# typeid(void*).name() --> Pv
$ c++filt --format=gnu-v3 Pv
void*

# void f1(int);
# typeid(&f1).name() --> PFviE
$ c++filt --format=gnu-v3 PFviE
void (*)(int)
--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
David List
2003-08-08 18:15:04 UTC
Permalink
On Fri, 08 Aug 2003 11:21:30 -0500,
Post by Jim Fischer
The string value returned from the .name() method is implementation
defined. In other words, there is no requirement that
typeid(int).name()
return the string "int". So there is nothing "wrong" with g++ 3.3
returning the string "i" instead of "int" here.
Darn, I understood from Stroustrup that it was only the persistance
part of the const char* returned from name() method that was
implementation defined - in other words, how and where exactly the
string was in memory. I didn't look into the standard, though.
Post by Jim Fischer
FWIW, you can use the 'c++filt(1)' program to "demangle" the string
# typeid(int).name() --> i
$ c++filt --format=gnu-v3 i
int
# typeid(void*).name() --> Pv
$ c++filt --format=gnu-v3 Pv
void*
# void f1(int);
# typeid(&f1).name() --> PFviE
$ c++filt --format=gnu-v3 PFviE
void (*)(int)
Hmm...I could look into that.

Have you used c++filt from within your own C++ code?

Thank you for your answer.
--
Med venlig hilsen / Best regards
David List
David List
2003-08-08 19:58:03 UTC
Permalink
On Fri, 08 Aug 2003 14:23:08 -0500,
Post by David List
Have you used c++filt from within your own C++ code?
'c++filt' is a stand-alone executable program that "knows" how to
demangle low-level symbols in Java and C++ executables created with the
GNU Compiler Collection (specifically, gcj and g++). The c++filt program
is not something that can be incorporated into C++ source code.
OK.
Thank you for your answer.
--
Med venlig hilsen / Best regards
David List
Jim Fischer
2003-08-13 05:21:12 UTC
Permalink
Post by David List
Have you used c++filt from within your own C++ code?
'c++filt' is a stand-alone executable program that "knows" how to
demangle low-level symbols in Java and C++ executables created with
the GNU Compiler Collection (specifically, gcj and g++). The c++filt
program is not something that can be incorporated into C++ source code.
http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html
Thanks for the info! Here's a little demo program for anyone who's
interested:

<example>

<code>
#include <cstdlib>
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>

#define FREE(p) std::free(p); p = 0

void foo(int) { }

int main()
{
int status;
size_t length;
const char *p1 = 0;
char *p2 = 0;

p1 = typeid(&foo).name();
p2 = abi::__cxa_demangle(p1, 0, &length, &status);
std::cout << p1 << " : " << (p2 ? p2 : p1) << '\n';
FREE(p2);

p1 = typeid(length).name();
p2 = abi::__cxa_demangle(p1, 0, &length, &status);
std::cout << p1 << " : " << (p2 ? p2 : p1) << '\n';
FREE(p2);

return 0;
}
</code>

<output>
PFviE : void (*)(int)
j : unsigned
</output>

</example>

n.b. I used G++ 3.3.1 to build this code.
--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Continue reading on narkive:
Loading...