I have a vector class which has some static const variables like ZERO
. Now since vector is often implemented as a template class (and mine is no exception), I see a lot of this code:
template<> const Vector2<float> Vector2<float>::ZERO;
template<> const Vector2<float> Vector2<float>::UNIT_X(1, 0);
//... and so on, and then all code duplicated for other types (int, double, long double)
// including different sizes of the Vector (Vector2, Vector3, Vector4)
My question is, can I do something like this instead to avoid duplicating code just for a different type:
template <typename T, unsigned int SIZE>
const Vector<T, SIZE> Vector<T, SIZE>::ZERO;
Can that satisfy all future types? If not, will it make a difference if I put the following to explicitly define the classes for the various types:
template Vector<float, 2>;
template Vector<float, 3>;
So far, I have tested it on Visual C++ (2008) and it compiles fine and the tests pass, but I am wondering if this is non-standard code.
See Question&Answers more detail:os