Templates and generic programming
T.80
Do not naively templatize a class hierarchy
Reason
Templating a class hierarchy that has many functions, especially many virtual functions, can lead to code bloat.
Example, bad
template<typename T>
struct Container { // an interface
virtual T* get(int i);
virtual T* first();
virtual T* next();
virtual void sort();
};
template<typename T>
class Vector : public Container<T> {
public:
// ...
};
Vector<int> vi;
Vector<string> vs;
It is probably a bad idea to define a sort as a member function of a container, but it is not unheard of and it makes a good example of what not to do.
Given this, the compiler cannot know if vector<int>::sort() is called, so it must generate code for it. Similar for vector<string>::sort(). Unless those two functions are called that's code bloat. Imagine what this would do to a class hierarchy with dozens of member functions and dozens of derived classes with many instantiations.
Note
In many cases you can provide a stable interface by not parameterizing a base; see "stable base" and OO and GP
Enforcement
- Flag virtual functions that depend on a template argument. ??? False positives