The way I'd do it is:
/* begin C++ */
template<typename T>
class TypeTraits {
public:
static const char* name() { return "unknown"; }
};
template<>
class TypeTraits<float> {
public:
static const char* name() { return "float"; }
};
// add more type traits as you need them
template<typename T>
class myclass {
public:
static const char* getT() { return TypeTraits<T>::name(); }
};
/* end C++ */
I don't know if I remember the exact syntax for partial template specialization, but that should be
close enough. :)
And no, there isn't an easy way to make it automatically work. You do have to make new type traits
for new types.
> is it possible to get a string version of the class used as a template
> parameter?
>
> for example if i have
>
> template <class p>
> class myclass
> {
> // return the string version of p
> static std::string getP() { return ??????; }
> };
>
> if I call myclass<float>::getP(), then it should return the string "float"
>
> how to implement getP ???
--
Chad Austin
http://aegisknight.org/
|