One possible way:
#include <cstdio>
// forward declaration
template<typename>
class A;
template<typename>
class A_Base {
// general case
public: void f() { printf("A<T>::f\n"); }
};
// partial specialization for any A
template<typename T>
class A_Base< A<T> > {
// special stuff
public: void f() { printf("A<A<T>>::f\n"); }
};
template<typename T>
class A : private A_Base<T> { // private inheritance, we're not modeling IS-A
public:
using A_Base<T>::f; // make f accesible
// all the other stuff that doesn't need to change
};
int main()
{
A<int> a1;
A<A<int>> a2;
a1.f(); // A<T>::f
a2.f(); // A<A<T>>::f
}
This way you put only the functions that need to behave differently in the base class. You can of course make it vice-versa - you put common code in the base class and specialize a derived class where you also define f
.