I have a generic template class Vector
. The intended use is for the user to specialize the generic Vector
and define any custom operations. For example,
using IntVector = Vector<int>;
IntVector operator+ (const IntVector& a, const IntVector& b);
The issue is that there are some operations you can't define as free-functions, like IntVector& operator+= (const IntVector& b);
. To work around this you can specialize through inheritance,
class IntVector : public Vector<int> {
public:
IntVector& operator+= (const IntVector& b);
};
IntVector operator+ (const IntVector& a, const IntVector& b);
There is an issue with this though, the slice operator defined on the base template class returns a generic Vector_view
instead of the specialized IntVector_view
that supports operator+=
. You would have to overload the slice operator with the same body as the base class, just a different return type, which is obnoxious.
This is a related question where it is recommends making a wrapper object, which is rather tedious for each concrete type. Is there any easy way to create a specialization where you can add new interfaces without having to redefine the type? If that is the only answer, I don't see a point in creating a generic base class.