V
Vincent Cantin
I have a class defined by a template which needs to "say" its type to the
user via string.
As an example, here is the class that I want to fix :
template<class T> class Container : public Serializable
{
public:
T value;
static string classIdentifier;
string& getClassIdentifier();
};
template <class T> string& Container<T>::getClassIdentifier()
{
return classIdentifier;
}
For now, the only way I found to initiliaze the string is with :
string Container<bool>::classIdentifier("Container<bool>");
string Container<char>::classIdentifier("Container<char>");
string Container<short>::classIdentifier("Container<short>");
string Container<int>::classIdentifier("Container<int>");
string Container<float>::classIdentifier("Container<float>");
string Container<double>::classIdentifier("Container<double>");
string Container<unsigned char>::classIdentifier("Container<unsigned
char>");
string Container<unsigned short>::classIdentifier("Container<unsigned
short>");
string Container<unsigned int>::classIdentifier("Container<unsigned int>");
string Container<unsigned float>::classIdentifier("Container<unsigned
float>");
string Container<unsigned double>::classIdentifier("Container<unsigned
double>");
This solution works for the basis types, but now imagine that I would like
to use my template class with another type which is not in the list ? The
compiler will say that my static variable is not defined :-(
I think there might be a way to do it with a macro but I don't know how to
do. Does someone here have a solution ?
Vincent
user via string.
As an example, here is the class that I want to fix :
template<class T> class Container : public Serializable
{
public:
T value;
static string classIdentifier;
string& getClassIdentifier();
};
template <class T> string& Container<T>::getClassIdentifier()
{
return classIdentifier;
}
For now, the only way I found to initiliaze the string is with :
string Container<bool>::classIdentifier("Container<bool>");
string Container<char>::classIdentifier("Container<char>");
string Container<short>::classIdentifier("Container<short>");
string Container<int>::classIdentifier("Container<int>");
string Container<float>::classIdentifier("Container<float>");
string Container<double>::classIdentifier("Container<double>");
string Container<unsigned char>::classIdentifier("Container<unsigned
char>");
string Container<unsigned short>::classIdentifier("Container<unsigned
short>");
string Container<unsigned int>::classIdentifier("Container<unsigned int>");
string Container<unsigned float>::classIdentifier("Container<unsigned
float>");
string Container<unsigned double>::classIdentifier("Container<unsigned
double>");
This solution works for the basis types, but now imagine that I would like
to use my template class with another type which is not in the list ? The
compiler will say that my static variable is not defined :-(
I think there might be a way to do it with a macro but I don't know how to
do. Does someone here have a solution ?
Vincent