I have a class C which is templated on A<W> or on B<W>. Now in C, I’d like to construct an object of type A<U> or B<U>, depending on what it was instantiated with.
If that sounds a bit strange, consider this code and the comment in it:
template<class W>
struct A {
typedef A type;
};
template<class W>
struct B {
typedef B type;
};
template<class AB>
struct C {
// AB is A or B. If it's A we want to construct A<double>, if it's B
// we want to construct B<double>:
typedef typename AB::type type; // A or B
typename type<double> D; // ERROR
D d;
};
int main(int argc, char** argv){
C<A<int> > c1;
C<B<int> > c2;
}
Is there any way to do this?
I think C would need to be templated on a nested template, but I’m not sure how to do it.
To do that, you need partial template specifications:
A more general case that works for any template class with one type argument would be: