Topic: turning a template parameter into a character string
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/05 Raw View
leblanck@austin.ibm.com (Karalee LeBlanc) writes:
>I need to be able to take a template parameter, and turn it into a
>character string. So, for example, if I have
>
>template <class AAA> void MyTmpl(AAA var) { .... }
Use `typeid(AAA).name()'. You will need a compiler that supports RTTI.
>I'm working with a compiler that doesn't have RTTI
>yet, so that's out.
In that case, you are out of luck.
The only possibility is to roll your own RTTI.
For example, you could use template specializations:
template <class T>
class my_typeinfo {
static const char *name();
};
// repeat the following for every type of interest -
// you could use a macro to automate this a bit more
class my_typeinfo<int> {
static const char *name();
};
const char *my_typeinfo<int>::name() { return "int"; }
Then use `my_typeinfo<AAA>::name()'.
--
Fergus Henderson | I'll forgive even GNU emacs as
fjh@cs.mu.oz.au | long as gcc is available ;-)
http://www.cs.mu.oz.au/~fjh | - Linus Torvalds
Author: rjl@f111.iassf.easams.com.au (Rohan LENARD)
Date: 1995/05/06 Raw View
In article <D80sCL.JwH@austin.ibm.com>,
Karalee LeBlanc <leblanck@austin.ibm.com> wrote:
:
: I need to be able to take a template parameter, and turn it into a
:character string. So, for example, if I have
:
:template <class AAA> void MyTmpl(AAA var) { .... }
:
I believe you could use typeid to help you here.
#include <typeinfo.h>
template <class T> const char* GetTypeName(const T& a) {
return typeid(a).name();
}
Unfortunately I couldn't find anything in the Feb Draft of the working paper
which said that the names returned had to mean anything. ie. GetTypeName
(const int& a) returns a string of "const int&"
:And an example of using it is:
:
:int tmpA;
:MyTmpl(tmpA);
:
:I need to, inside the logic of MyTmpl, be able to generate character
:string: "int". I'm working with a compiler that doesn't have RTTI
:yet, so that's out. I've tried:
:
: "AAA"
: #AAA // Yes, I know this isn't a macro, but it was worth a shot.
:
This can't ever work, because # is done during preprocessing and template
expansion occurs later in the compilation process.
It is a nice extension that I'm sure many would like
Regards,
Rohan
--
----------------------------------------------------------------------------
rjl@iassf.easams.com.au | All quotes can be attributed to my automated quote
Rohan Lenard | writing tool. Yours for just $19.95; and if you
+61-2-367-4555 | call now you'll get a free set of steak knives ...
Author: leblanck@austin.ibm.com (Karalee LeBlanc)
Date: 1995/05/03 Raw View
I need to be able to take a template parameter, and turn it into a
character string. So, for example, if I have
template <class AAA> void MyTmpl(AAA var) { .... }
And an example of using it is:
int tmpA;
MyTmpl(tmpA);
I need to, inside the logic of MyTmpl, be able to generate character
string: "int". I'm working with a compiler that doesn't have RTTI
yet, so that's out. I've tried:
"AAA"
#AAA // Yes, I know this isn't a macro, but it was worth a shot.
and some other variations, but I always end up with the character
string being "AAA" not "int". Any suggestions?
--
-----------------------------------------------------
Karalee LeBlanc
Dept 908, Bld 008, Room 7D050
Austin, Tx. Tie/Phone 678/838-3409