1

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
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?>;
deepak4bin
  • 810
  • 9
  • 13

4 Answers4

3

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> >();
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    I don't think it parses in *any* C++ version. An explicit instantiation requires to write `class` before the class name, I think. Also, what purpose do you want to achieve?? – Johannes Schaub - litb May 06 '11 at 12:50
2

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;
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
2

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;
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
1

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> >.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Note that C++03 compilers are *required* to see `>>` as shift operator, while C++0x (probably going to be C++11) compilers are/will be required not to need the space here. – Christopher Creutzig May 06 '11 at 13:02
  • @Christopher - The compilers I have used are not that strict, and seems to sort out the difference from context. – Bo Persson May 06 '11 at 13:24
  • Most compilers do not really follow any standard by default, and very few do so even with their “strict” flags. Still, this is a very well-known deficiency of the C++03 standard, so I sure hope your compilers at least clearly spell out in their documentation that they violate the spec in this regard. (Not that I have a big problem with that in this particular case.) – Christopher Creutzig May 06 '11 at 13:55