类模板实例化为类型模板参数,语法?

时间:2014-10-22 21:33:43

标签: c++ class templates syntax type-parameter

class XY{};

template<typename typeA>
class A
{
(...)
};
template<typename typeB>
class B
{
(...)
};
(...)
     B<class <class XY>A> * attribute; // <- How can I do that without Syntaxerror

尝试此gcc时会出现以下错误:

  

xy.h:19:错误:模板参数1无效

我该如何避免?

2 个答案:

答案 0 :(得分:3)

class关键字仅用于定义模板类,而不用于声明对象。为此,您只需要:

B<A<XY> >* attribute;

或者为了清楚起见将其展开:

typedef A<XY> MyA;
typedef B<MyA> MyB;
MyB* attribute;

答案 1 :(得分:1)

你的问题很不清楚,但我认为你是在模板模板参数之后。这样:

template <template <class> class U>
class Foo {};

现在Foo是一个类模板,接受另一个类模板作为其参数,如下所示:

template <class V>
class Bar {};

Foo<Bar> theFoo;
相关问题