Specializing nested templates

77 views Asked by At

I have a class template A which looks like this. Consider TImpl1 and TImpl2 as arguments that define the implementations of abstract data types B and C to solve a problem that belongs to A:

template <typename TImpl1, typename TImpl2, typename TImpl3>
A<B<TImpl1, TImpl2>, C<TImpl3> >

I also have a lot of non-member functions that get an instance of A, call each other and do something with A. Now I want to change the specification of a single method for a specific kind of A-classes that I'm going to introduce.

The easy way: I introduce an empty struct "SpecialClass" and do partial specialization:

struct SepcialClass;

// method already existed
template <typename TImpl1, typename TImpl2, typename TImpl3>
method1024(A<B<TImpl1, TImpl2>, C<TImpl3> > a) {
   ...
}

// new method for only some 
template <typename TImpl1, typename TImpl2>
method1024(A<B<TImpl1, TImpl2>, C<SpecialClass> > a) {
   ...
}

So whenever I declare A<B<...>,C<SpecialClass> >, all the methods run properly and for method1024 the specialized one is chosen.

This totally works, but: I lost the possiblity to specify the implementation of C.

Unfortunately I cannot redesign A by adding a third argument to it.

Are there any other ideas / workarounds for this kind of problem?

1

There are 1 answers

1
Jay Miller On

What about this?

template <class T>
struct SpecialClass : T {};

template <class T>
void method1024(A<B<TImpl1, TImpl2>, C<SpecialClass<T>>> a) {
   ...
}

This overload is specialized on any SpecialClass, which in turn is derived from some other type, so you could pass a C<SpecialClass<TImpl3>>, for example, and your C implementation has still been specified (since SpecialClass is of type TImpl3).