如何声明模板类的模板
how to declare template of template class
如何声明模板类的模板?? 请参阅下面的代码:
File: A.h
class A
{
...
...
};
File: B.h
template <typename U>
class B
{
...
...
};
File C.h
template <class T>
class C
{
...
...
};
File C.cpp
//In this file I am able to write template declaration for class A(non-template class)
#include "A.h"
template C<A>; //this works fine.
How can I write the same for class B(which is a template class.)
#include "B.h"
template C<What should I write here for class B?>;
如果你想
使用B
作为C
类模板的类型参数,那么你可以这样写:
template class C<B<int> >; //i.e provide the some type B template class
//^^^^ this is needed for explicit instantiation!
C<B<A> > variable;
C<B<short> > *pointer = new C<B<short> >();
C
模板需要非模板类型,以便它可以生成类。这就是为什么C<A>
有效:A
不是模板。但是,C<B>
不起作用,因为B
只是类型本身的模板。您需要具有某种类型来实例化B
模板。
例如,这可以工作:
C<B<A> > x;
你可以这样写:
C< B< A > > c;
如果你觉得这令人困惑,那么你可以使用typedef:
typedef B<A> MyB;
C<MyB> c;
好吧,B 也是一个模板,所以像 C<B<A>>
这样的东西会起作用。
请注意,某些编译器将 >>
视为移位运算符,并且需要 C<B<A> >
。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 在函数声明之后声明整数有什么用?
- 工会成员中的模板 - 声明没有声明任何内容
- 你能在函数声明中声明一个固定长度的数组吗?
- 外部模板:声明不声明任何内容
- 单个CPP文件中多次声明函数声明可以吗?
- 恒定声明全局或在使用它的函数中声明恒定声明之间有什么区别?
- 在C++中声明可见性/访问权限的方法有何不同
- 我们可以在类声明中声明类类型的指针吗?
- 关于全局变量的外部声明"C"外部声明
- 声明不声明任何[-fpermission]错误
- Cygwin g++和声明不声明任何内容
- 错误:声明不声明
- 使用声明是否仅导入在使用声明上方声明的重载
- Cython 错误:声明不声明任何内容
- clang与gcc在查找声明方面有何不同
- 在类声明中声明指向类对象的指针
- 基于范围的循环声明必须声明变量
- 如何使用ADT和链表声明未声明的标识符
- 初始化const类成员,错误:声明没有声明任何内容