473,418 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

unscrambling typeid().name

I'm using a templatized class in GCC based environ

template<class A, class B>
class foo {...
....} F;

Now I want to know that particular instance of this class was
instantiated with what template arguments. typeid.name() returns a
strange string with all the info but abbreviated and probably platform
specific.
How can I unscramble it and probably use it my program during runtime?

TIA
Sep 30 '08 #1
7 5574
Deepak Jharodia wrote:
I'm using a templatized class in GCC based environ

template<class A, class B>
class foo {...
...} F;

Now I want to know that particular instance of this class was
instantiated with what template arguments. typeid.name() returns a
strange string with all the info but abbreviated and probably platform
specific.
How can I unscramble it and probably use it my program during runtime?
You don't. The format of the string is "vendor specific", meaning that
if you want portability you can't use it other than the property that
it's probably unique.

typedef std::pair<const std::type_info *, const std::type_info *types;

How about methods on foo that give you the type_info's for A and B ?

template<class A, class B>
class foo {
virtual std::type_info& typeA() {
return typeid(A);
}
virtual std::type_info& typeB() {
return typeid(B);
}
};

Or, you can create a map of type_info of foo<A,Bto A and B if you
really need it.

Or if you need specific information about A and B you can construct some
other way of programatically getting type information of A and B
respectively.

Doing any of these will be portable but parsing the string will not be.
Sep 30 '08 #2
On Sep 30, 11:57 am, Gianni Mariani <gi4nos...@mariani.wswrote:
Deepak Jharodia wrote:
I'm using a templatized class in GCC based environ
template<class A, class B>
class foo {...
...} F;
Now I want to know that particular instance of this class
was instantiated with what template arguments. typeid.name()
returns a strange string with all the info but abbreviated
and probably platform specific.
How can I unscramble it and probably use it my program
during runtime?
You don't. The format of the string is "vendor specific",
meaning that if you want portability you can't use it other
than the property that it's probably unique.
I wouldn't count on it for types not defined at namespace scope.
(Of course, you can't instantiate templates over such types, so
that shouldn't be a problem for the original poster.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 30 '08 #3
On Sep 30, 3:01 am, Deepak Jharodia <deepakjharo...@gmail.comwrote:
I'm using a templatized class in GCC based environ

template<class A, class B>
class foo {...
...} F;

Now I want to know that particular instance of this class was
instantiated with what template arguments. typeid.name() returns a
strange string with all the info but abbreviated and probably platform
specific.
How can I unscramble it and probably use it my program during runtime?

TIA
Why would you need to? State your case in simplified, compileable
code.

A template is not a class.
Say you instantiate a template with foo< int, char >. Why do you need
to know what the first injected type parameter is/was? The fact that
its an integer should be transparent to the code.

example:

template<class A, class B>
class foo
{
A a;
B b;
public:
foo() : a(), b() { }
A get() const { return a; }
};

// foo< int, char >, the following class is silently generated
// (i beleive the standard calls it template instantiation)

template<>
class foo
{
int a;
char b;
public:
foo() : a(), b() { }
int get() const { return a; }
};

Now take foo< int, char and foo< char, int as a theoretical case.
Each of these are unique types. There is no ambiguity between the two
types.

Now, if you come from the Java world where everything and anything is
an Object, and you are trying to mimic the same hierarchy with C++,
then prepare yourself for a reality check.
Sep 30 '08 #4
On Sep 30, 3:10*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Sep 30, 3:01 am, Deepak Jharodia <deepakjharo...@gmail.comwrote:
I'm using a templatized class in GCC based environ
template<class A, class B>
class foo {...
...} F;
Now I want to know that particular instance of this class was
instantiated with what template arguments. typeid.name() returns a
strange string with all the info but abbreviated and probably platform
specific.
How can I unscramble it and probably use it my program during runtime?
TIA

Why would you need to? State your case in simplified, compileable
code.

A template is not a class.
Say you instantiate a template with foo< int, char >. Why do you need
to know what the first injected type parameter is/was? The fact that
its an integer should be transparent to the code.

example:

template<class A, class B>
class foo
{
* A a;
* B b;
public:
* foo() : a(), b() { }
* A get() const { return a; }

};

// foo< int, char >, the following class is silently generated
// (i beleive the standard calls it template instantiation)

template<>
class foo
{
* int a;
* char b;
public:
* foo() : a(), b() { }
* int get() const { return a; }

};

Now take foo< int, char and foo< char, int as a theoretical case.
Each of these are unique types. There is no ambiguity between the two
types.

Now, if you come from the Java world where everything and anything is
an Object, and you are trying *to mimic the same hierarchy with C++,
then prepare yourself for a reality check.
Oh I forgot to mention that I'm not the owner of the class in that, I
can not change it in anyway.

What I really want to know is that whether a specific pointer to the
mentioned class(say A) is inherited from a specific class(say B, which
is also templatized).
Now I try to do a dynamic_cast on the pointer for this, but for that I
need to know what template arguments were used while allocating this
pointer(of type A) because they will be same as the used for type B.

I hope I was able to make it a bit clear.
Sep 30 '08 #5
On Sep 30, 3:08*pm, James Kanze <james.ka...@gmail.comwrote:
On Sep 30, 11:57 am, Gianni Mariani <gi4nos...@mariani.wswrote:
Deepak Jharodia wrote:
I'm using a templatized class in GCC based environ
template<class A, class B>
class foo {...
...} F;
Now I want to know that particular instance of this class
was instantiated with what template arguments. typeid.name()
returns a strange string with all the info but abbreviated
and probably platform specific.
How can I unscramble it and probably use it my program
during runtime?
You don't. *The format of the string is "vendor specific",
meaning that if you want portability you can't use it other
than the property that it's probably unique.

I wouldn't count on it for types not defined at namespace scope.
(Of course, you can't instantiate templates over such types, so
that shouldn't be a problem for the original poster.)

--
James Kanze (GABI Software) * * * * * * email:james.ka...@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
So you say it will be portable(across compilers)??
I'll be using gcc(though diff versions) only on linux.
Sep 30 '08 #6
Deepak Jharodia wrote:
I'm using a templatized class in GCC based environ

template<class A, class B>
class foo {...
...} F;

Now I want to know that particular instance of this class was
instantiated with what template arguments. typeid.name() returns a
strange string with all the info but abbreviated and probably platform
specific.
How can I unscramble it and probably use it my program during runtime?
For recent versions of GCC, __cxa_demangle unscrambles the typename.
Please see the manual:
http://gcc.gnu.org/onlinedocs/libstd...1pt12ch39.html

Sep 30 '08 #7
On Sep 30, 1:37 pm, Deepak Jharodia <deepakjharo...@gmail.comwrote:
On Sep 30, 3:08 pm, James Kanze <james.ka...@gmail.comwrote:
On Sep 30, 11:57 am, Gianni Mariani <gi4nos...@mariani.wswrote:
Deepak Jharodia wrote:
I'm using a templatized class in GCC based environ
template<class A, class B>
class foo {...
...} F;
Now I want to know that particular instance of this class
was instantiated with what template arguments. typeid.name()
returns a strange string with all the info but abbreviated
and probably platform specific.
How can I unscramble it and probably use it my program
during runtime?
You don't. The format of the string is "vendor specific",
meaning that if you want portability you can't use it other
than the property that it's probably unique.
I wouldn't count on it for types not defined at namespace scope.
(Of course, you can't instantiate templates over such types, so
that shouldn't be a problem for the original poster.)
So you say it will be portable(across compilers)??
I'll be using gcc(though diff versions) only on linux.
You can probably count on the names being unique for all types
defined at namespace scope. The exact name for a given class
will vary from one compiler to the next, however; for a class A
in global namespace, g++ outputs "1A", Sun CC "A", and VC++
"class A", if the definition is in namespace NS, g++ outputs
"N2NS1AE", Sun CC "NS::A" and VC++ "class NS::A".

For those who are curious, I'm using the following code for test
purposes:

typeid.cc:

#include <typeinfo>
#include <iostream>

static void
g()
{
{
class A {} ;
std::cout << typeid( A ).name() << std::endl ;
}
{
class A {} ;
std::cout << typeid( A ).name() << std::endl ;
}
}

class A
{
} ;

namespace NS { class A {} ; }

int
main()
{
extern void f() ;
std::cout << typeid( A ).name() << std::endl ;
std::cout << typeid( NS::A ).name() << std::endl ;
g() ;
f() ;
return 0 ;
}

typeid2.cc

#include <typeinfo>
#include <iostream>

static void
g()
{
{
class A {} ;
std::cout << typeid( A ).name() << std::endl ;
}
{
class A {} ;
std::cout << typeid( A ).name() << std::endl ;
}
}

void
f()
{
g() ;
}

(Note that the definitions of class A in g() are at exactly the
same line in both files---VC++ encodes the line number of a
local class in its name, so they only have the same name if they
are defined on the same line.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 1 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Andreas Sch. | last post by:
Hello, I had been quite suprised by the following little program: ---- cut ---- #include "iostream.h" class base {
3
by: Mike | last post by:
I want to use typeid() in a base class function to determine the name of the derived class. typeid(this) returns the name of the base class (which is an abstract class) rather than the derived...
3
by: Max | last post by:
I am trying to find a way to eliminate vararg functions from my code by packaging the input parameters in stringstreams. Here is an oversimplified example of what I am trying to do: Functions...
19
by: Marco Jez | last post by:
Hi everyone! I would like to use the reference returned by typeid as key in a std::map. Is it safe to assume that typeid(T) (where T is a type name) will always return the same reference to the...
2
by: Generic Usenet Account | last post by:
My C++ compiler (gcc 3.3.1) is prefixing a number in front on a user-defined structure when I invoke typeid() on it. Can anyone explain why? Source code follows: Bhat ...
17
by: Michael Olea | last post by:
Sometimes, in an introspective mood, my code reflecting on itself, I like to write test code that prints the results of introspection, some of which consist of the names of Types. The problem is:...
1
by: Gil Grissom | last post by:
Hi, I have a problem with Qt and the use of the rtti features. I compiled Qt and the included examples with the intel c++ compiler 8.1 like the following commands show: icl -c -nologo...
18
by: Adam Zimny | last post by:
This is fragment of code from Bruce Eckel's Thinking in c++ ( last 3 couts are mine to show what happened ). The question is: is Bruce Eckel wrong or g++ ( my version is 3.2.3 ) is buggy ? //:...
4
by: michael | last post by:
Hi All, I have the following: class ClassA { }; class ClassB { };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.