In other words: Is it possible to make a template specialisation that inherits from its base, like this:
template <class T>
class A{};
template <>
class A<int>:public A<>{};
so that A has all of A’s functions?
I’m new here, so I dunno how to format, just in case the code comes up incorrectly.
You can, with a bit of trickery. This pattern is sometimes called “template subclassing” and is used extensively in the SeqAn library.
The trick is to give the base class an additional template argument tag which determines the type identity:
Here,
voiddenotes the base (you could also use a dedicated tagBasebutvoidworks fine) andDerived, an empty struct, denotes the derived class.Now you can instantiate and use the templates as follows:
For a real-world example, consider the
Stringclass from SeqAn:The second type derived from the first one, but is a specialisation which packs its characters as tightly as possible (for DNA, this means putting four DNA characters in each byte).