我有一个类C
这是模板A<W>
或B<W>
。现在在C
中,我想要构造一个类型为A<U>
或B<U>
的对象,具体取决于它实例化的内容。如何在收到Foo <S>后构造Foo <T>?
如果这听起来有点怪,考虑下面的代码,并在它的评论:
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;
}
有没有办法做到这一点?
我认为C
需要模板化嵌套模板,但我不知道该怎么做。
啊,这就是我在脑子里,当我说用它或许可以得到解决嵌套模板。太好了! – Frank