To define a specialization that is used for every Vector of pointers and only for
Vectors of pointers, we need a partial specialization:
template <class T> class Vector <T *> : private Vector<void *> {
public:
typedef Vector<void*> Base;
Vector(): Base() {}
explicit Vector(int i) : Base(i ) {}
T *& elem(int i ) { return static_cast <T *&> (Base::elem(i)); }
T *& opeator[](int i) { return static_cast <T *&>(Base::operator[](i )); }
//...
};
This definition has me in a tizzy. This is related to partial specialization but i don’t understand the syntax. private Vector<void *> definition part looks like a parent class to me.
- Why not specify
Vector <void *>intemplate <class T> class Vector <void *>. - It would be great if anybody can breakdown the definition part. (sorry if its too much to ask)
Forget about the inheritance, which has nothing to do with the problem at hand.
Partial specialization means that you make a new template from an existing one which is more specialized, but still generic, by matching a more restrictive pattern. The general pattern of your example is like this:
The first line is the primary template and matches everything that is not matched by a more specialized form. The third line defines an actual type (not a template!)
Foo<fool>(for some given typefool). The middle line, on the other hand, is still a template, but it only matches a type of the formT = U *, i.e. a pointer:About the
Vector<void*>: It just turns out that the author chooses to define the partially-specializedVector<U*>as deriving from a fixed classVector<void*>(which would have to be fully specialized elsewhere).