Table of Contents
The C++ Standard Template Library forms the basis of modern C++ design, however, it features free-function design, and is not designed for inheritance. This header-only library provides minimal inheritance-safe wrappers.
- Use protected inheritance.
- Export all member types, variables, and functions
- Add trivial virtual destructor.
- Overload non-member functions in terms of the base class
struct StdIntVector: public std::vector<int> {};
std::vector<int> *ptr = new StdIntVector;
delete ptr; // undefined behavior
...
struct ItlIntVector: public itl::vector<int> {};
std::vector<int> *ptr = new ItlIntVector; // fails to compile
itl::vector<int> *ptr = new ItlIntVector;
delete ptr; // safe
Each inheritable wrapper will add a small amount of overhead (typically a pointer to a vtable, or 4-8 bytes) to the STL container, as well as incur a small runtime penalty for object destruction. However, all wrapped methods should be inlined by the compiler, making the total overhead minimal.
Public Domain, see license.