Adding a base class in c++ template specialization
In the dusty corners of a code base I work on, I've come across a class
hierarchy that looks like this:
class Base1
{
int base1;
};
class Base2
{
int base2;
};
template <typename T> class A : public Base1
{
T _specialT;
};
template <> class A<int> : public Base2
{
int _special;
};
The use of Base2 in the specialization of A is something that surprised
me. I've been searching around to understand exactly what it means,
haven't been able to find any examples or discussions of this sort of
design.
It seems to me that what this does is cause A to inherit from both Base1
and Base2 while other uses of A that are not specialized will inherit from
only Base1. That leaves me with a couple of questions:
Do I understand this correctly?
Are there any non-obvious caveats in added to the hierarchy this way? Why
would you do this?
Are there times when this would be considered good design?
No comments:
Post a Comment