James Kanze
10/1/2008 7:50:00 AM
On Sep 30, 1:37 pm, Deepak Jharodia <deepakjharo...@gmail.com> wrote:
> On Sep 30, 3:08 pm, James Kanze <james.ka...@gmail.com> wrote:
> > On Sep 30, 11:57 am, Gianni Mariani <gi4nos...@mariani.ws> wrote:
> > > 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:james.kanze@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