Hi,
I have a template class which is used to store variables and then print them as strings
what I want is to have my class produce the string to represent the type automatically
Currently an instance of the class must be declared as
CValue<int> foo("Name", "int", 56);
I would like to reduce that to
CValue<int> foo("Name", 56);
the ouput from both functions should be
<Name type="int">56</Name>
This means having a function to get the "int" from <int>
what I was trying is
/////////////////////////////////////
template <class _T>
std::string GetType(_T me)
{
if (int i = dynamic_cast<int>(me))
{
return std::string("int");
}
else if(char c = dynamic_cast<char>(me))
{
return std::string("char");
}
return "";
}
///////////////////////////////////////////
this of course doesn't work since dynamic_cast isn't designed for that sort of thing
Can anyone give me a way of getting this function to work?
i.e. a function like below
int a;
std::string s = GetType(a);
// s = "int"
char c;
s = GetType(c);
// s = "char"
Vin
#include <iostream>
#include <string>
using namespace std;
template <class T>
string Info(const T& t)
{
string result;
const type_info& i = typeid(t);
result = i.name();
return result;
}
int main(int argc, char* argv[])
{
int c = 3;
cout << Info(c) << endl;
return 0;
}