Original Post
For a while, I've been looking for a way to get the name (as a c-string) of a template parameter, without using RTTI. Speaking with digital, he mentioned the idea of using the __FUNCTION__ macro and some string parsing, so I had a go and it seems to work quite easily.
This is pretty hacky, so I'm hoping some of you fellow C++ users can tear apart it's weaknesses and expose the down-sides for me.
My first concern is that this relies on MSVC's implementation of the __FUNCTION__ macro, which includes the class name. On GCC I'd have to use __PRETTY_FUNCTION__ instead. I'm assuming any decent compiler will have some kind of macro that I can use for this purpose...
Example usage:
This is pretty hacky, so I'm hoping some of you fellow C++ users can tear apart it's weaknesses and expose the down-sides for me.
My first concern is that this relies on MSVC's implementation of the __FUNCTION__ macro, which includes the class name. On GCC I'd have to use __PRETTY_FUNCTION__ instead. I'm assuming any decent compiler will have some kind of macro that I can use for this purpose...
template<class T>
struct TypeName
{
static void Get(const char*& begin, const char*& end)
{
begin = __FUNCTION__;
for(++begin; *begin && *(begin-1) != '<'; ++ begin);
for(end = begin; *end; ++ end);
for(; end > begin && *end != '>'; -- end);
}
};Example usage:
const char* begin=0, *end=0;
TypeName<Foo>::Get(begin, end);
assert(end>begin);
assert(end-begin < 128);
char buf[128];
memcpy(buf, begin, end-begin);
buf[end-begin] = 0;
printf(buf);//prints Foo