1). Why they are needed / i.e what problem do they solve ?
2). A simple example
I have read various articles etc, but it still dosen't seem to make
sense to me
template <class T, class Container>
template <class T, template <class> class Container>
with Stack1, the template parameter Container needs container class
with Stack2, Container is a template template parameter, it need a class
with *one* template parameter.
see the usage:
class Vector1; // holds int
template <class T>
Stack1<int, Vector1> s1;
Stack1<int, Vector2<int> > s2;
Stack2<int, Vector1> s3; // compile time error
Stack2<int, Vector2<int> > s4;
so template template parameter restrict the parameter you pass into the
Any other use, I don't know.
Anyway it's not mandatory, in STL, as far as I know, there's no template
template parameter usage.
> 2). A simple example
> I have read various articles etc, but it still dosen't seem to make
> sense to me
Stack2<int, Vector2> s4;
> Stack2<int, Vector2> s4;
I would add that if you can avoid code repetition like in Stack1 (the
parameter int is used twice), it is always better. Chances are that in
a more complicated and longer piece of code you use somehow
Stack1<int,Vector2<double> > instead Stack1<int,Vector2<int> > while
this mistake is avoided using the template template class.
> Could someone please explain template template classes, showing:
> 1). Why they are needed / i.e what problem do they solve ?
A counter example, where they are not used: std::allocator.
The standard containers allow to customize memory allocation by using
allocators. These allocators are classes where each one can allocate
memory for a _single_ type. In order to obtain an allocator for a
different type the nested "rebind" type has to be used. If the
containers would take a "template <typename> Allocator" parameter
instead of "typename Allocator" they could just use "Allocator<X>" or
"Allocator<Node>" without having to use the clumsy
Allocator:: template rebind<Node>::other
> but the /clumsy use/ of rebind is:
> Allocator:: template rebind<Node>::other
Well, even worse. But seems you got the point. ;)