Hi all -
I'm trying to do something which seems fairly simple, but is probably wrought with Peril.
I'm working on a TweakVar serialization system, and I'm starting with some templated code to print a value to a buffer. There's a little craziness because I'm really doing all the work through a templated wrapper class.
The idea is that people can provide their own specializations if they want to serialize new primitive types. This is supposed to be a little simple, only for tweakvars, not for say, the entire gamestate, which supports far more complex types and such...
// pre-declaration
template <class T> void PrintVal( const T* dat, char* buf, unsigned int bufSize );
//trimmed code for explaination
class ITweakVar
{
virtual void Print(char* buf, unsigned int bufSize)=0;
};
template <class T>
class TweakVarImpl<T>: public ITweakVar
{
T* pRuntimeVal; //set in a global ctor at startup
void Print( char* buf, unsigned int bufSize )
{
PrintVal<T>( pRunTimeVal, buf, bufSize );
}
};
//specializations
template <> void PrintVal<float> (const float* dat, char*buf, unsigned int bufSize)
{
unsigned int len = sprintf( "%f", buf, *val );
assert(len < bufSize-1);
}
template <> void PrintVal<int> (const int* dat, char* buf, unsigned int bufSize)
{
unsigned int len = sprintf( "%d", buf, *val );
assert(len < bufSize-1);
}
The ITweakVar class is some autoregistered/autolisted craziness that is tabulated so all the tweakvars can be dumped to a config file through a single function call ( in this case, by calling ITweakVar->Print() ).
The problem comes when a tweakvar is set to a char* (for use in strings, etc).
template <> void PrintVal<char*> (const char** dat, char* buf, unsigned int bufSize)
{
unsigned int len = sprintf( "%s", buf, *val );
assert(len < bufSize-1);
}
/*
Above doesn't compile, generates a
" void PrintVal<char*>(const char**, char*, unsigned int)
is not a specialization of a function template "
*/
Hmm, so function specializations of pointer types seems to be problematic. I'm considering the possibility of just using overloads instead of specializations, but I kinda prefer the template syntax - it just seems clearer, and seems less likely to generate unintended incorrect type conversions.
Thanks for any help!