First off, the code
vector<T> kvec;
for (ulong kv = 0; kv < ke.count; kv++)
{
T key;
if (typeid(T) != typeid(QUAT32))
{
fread(&key, sizeof(T), 1, fh);
}
else{
shortQuat key16;
fread(&key16, sizeof(shortQuat), 1, fh);
key.Parse(key16);
}
kvec.push_back(key);
}
the following compiler error is thrown at key.Parse(key16);
:
error C2039: 'Parse' : is not a member of 'libm2::VEC3F'
Of course it doesn't; but the problem is that anything with the type of VEC3F shouldn't even get there. Obviously the compiler is using the first type that gets sent to the class that snippet is from, which is fine - but why ignore the conditional?
And yeah, if I comment that line out, it compiles fine - and the conditional works fine at runtime.
So I guess the real question is, what's the best way to deal with this, without having to duplicate an entire class for one specialized type?
To expand on my comment:
The
if
runs at runtime, not compile time (the optimizer might remove the branch, but it has to compile in the first place to get that far).You can use template specialization instead, which is done at compile time (the compiler instantiates a separate copy of the templated function for each type you give it before compiling it). You can provide a specialization to force a different (special) function body for particular types, which is exactly what you want in this case.
Here's an example derived from the code in your question (I'm assuming the double-push_back to
kvec
for non QUAT32s is a typo):Problem: Needed to handle a special case in a templated constructor, that needed to be converted for usefulness. But, as I feared, if statements are ignored on compile.
Solution:
Add a second type to the template, and create a type-cast operator in one of the types.
In most cases, the class would be constructed like so:
obviously, casting between the same type should be pretty straightforward. But in the case of QUAT32...
the solution was to just do this:
and then the data that was stored in shortquat form could be cast to the more useful float format without issue.
It just seemed like a waste to have to create an entire duplicate just for a special case. I'd like to think it's the lesser of two ugly.
Another solution is to declare static variable in the template class, that stores previously defined type ID and check it when need to.