<Pe*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have the following in template class. I want doit() print the type
info. But it doesn't work. Could you please show me what is the correct
way to do that?
#include <iostream>
#define to_string( s ) # s
template <typename T>
class A {
public:
void doit(){
std::cout << to_string(T) << std::endl;
}
};
int main(int argc, char *argv[])
{
A<int> a;
a.doit();
}
You could do something like
#include <iostream>
#include <string>
using namespace std;
template <typename S>
struct StringType
{
static string name;
};
string StringType<int>::name = "int";
string StringType<string>::name = "string";
// etc //
// For some kinda default values
template <typename S>
String StringType<S>::name = typeid(S).name();
template <typename T>
struct A
{
void doit()
{
cout << StringType<T>::name << endl;
}
};
int main(int argc, char *argv[])
{
A<string> a;
a.doit();
}
this has the effect that when one wants to add a new type they get to create
the string for it, ofcourse one would like to have a default string for all
types but I can't see how to do this at the moment(whithout using typeid).
Unfortunately it seems, atleast in VS2k5, doesn't optimize out the
StringType names that are not used... atleast both names here showed up in
the retail's exe.