2

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.

Samaursa
  • 16,527
  • 21
  • 89
  • 160
  • 2
    It's a good idea to reserve ALL UPPERCASE names for macros. See just about any C++ FAQ. Using them for constants is a Java'ism, where you risk inadvertent text replacement. The Java convention came from original C, where constants "had" to be expressed as macro symbols. It's very silly to plug that back into C++. – Cheers and hth. - Alf Jun 18 '11 at 16:46
  • @Alf P. Steinbach: `"Using them for constants is a Java'ism, where you risk inadvertent text replacement"` Not sure I fully understand that. How do I risk text replacement? – Samaursa Jun 18 '11 at 16:53
  • someone else is likely to have defined a macro named ZERO. Besides, it's an eyesore. USING ALL UPPERCASE IS LIKE SHOUTING, don't do it. – Cheers and hth. - Alf Jun 18 '11 at 19:58
  • @Alf: Although ZERO is quite a bad choice for a macro (so is ATOM!) but I see your point. I will rename all the macro parameters in my code. Hopefully there is no specific guideline/rule in our coding manual for this. – Samaursa Jun 18 '11 at 20:20
  • 1
    It's OK. And besides, `template<> const Vector2 Vector2::ZERO;` does not define the variable. You need an explicit initializer for that (this was one of the drawbacks in C++03 but can be worked around in C++0x). – Johannes Schaub - litb Jun 18 '11 at 20:30

2 Answers2

1

No, that's perfectly legitimate and totally Standard. If you want to use a static variable in a templated class, there's no way you could possibly define all possible instantiations of it- those types may not even be nameable and therefore specializable. Hence, it's very necessary that template classes can have static variables defined for all possible uses.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • OK, I just found out that I get linking errors if I define the static variable in the source file (that is Vector.cpp) UNLESS I instantiate the class with the type that is using the static variables in the first place. Just thought I would clarify this for a future SO'er. – Samaursa Jun 18 '11 at 22:59
0

It's OK.

Templates are a kind of way to tell the compiler to generate similar code for different types.
This is exactly what it's for.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185