Templates and generic programming
T.84
Use a non-template core implementation to provide an ABI-stable interface
Reason
Improve stability of code. Avoid code bloat.
Example
It could be a base class:
struct Link_base { // stable
Link_base* suc;
Link_base* pre;
};
template<typename T> // templated wrapper to add type safety
struct Link : Link_base {
T val;
};
struct List_base {
Link_base* first; // first element (if any)
int sz; // number of elements
void add_front(Link_base* p);
// ...
};
template<typename T>
class List : List_base {
public:
void put_front(const T& e) { add_front(new Link<T>{e}); } // implicit cast to Link_base
T& front() { return static_cast<Link<T>*>(first)->val; } // explicit cast back to Link<T>
// ...
};
List<int> li;
List<string> ls;
Now there is only one copy of the operations linking and unlinking elements of a List. The Link and List classes do nothing but type manipulation.
Instead of using a separate "base" type, another common technique is to specialize for void or void* and have the general template for T be just the safely-encapsulated casts to and from the core void implementation.
Alternative: Use a Pimpl implementation.
Enforcement
???
T.var: Variadic template rules
???