how to declare template of template class
How can declare template of a template class?? see below code:
File: A.h
class A
{
...
...
};
File: B.h
template <typename U>
class B
{
...
...
};
File C.h
temp开发者_运维技巧late <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?>;
If you want to use B
as type argument to C
class template, then you can write this:
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> >();
The C
template expects a non-template type so that it can generate a class. That's why C<A>
works: A
is not a template. However, C<B>
doesn't work, since B
is only a template for a type itself. You need to have some type to instantiate the B
template.
For instance, this could work:
C<B<A> > x;
You can just write it like this:
C< B< A > > c;
If you find that confusing then you can use a typedef:
typedef B<A> MyB;
C<MyB> c;
Well, B is also a template, so something like C<B<A>>
would work.
Note that some compilers see >>
as a shift operator, and requires C<B<A> >
.
精彩评论