template template classes

A

Anonymous

Could someone please explain template template classes, showing:

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
 
B

Barry

Anonymous said:
Could someone please explain template template classes, showing:

1). Why they are needed / i.e what problem do they solve ?


template <class T, class Container>
class Stack1;

template <class T, template <class> class Container>
class Stack2

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>
class Vector2;

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
template class.
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.
 
B

Barry

Barry said:
template <class T, class Container>
class Stack1;

template <class T, template <class> class Container>
class Stack2

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>
class Vector2;

Stack1<int, Vector1> s1;
Stack1<int, Vector2<int> > s2;

Stack2<int, Vector1> s3; // compile time error
Stack2<int, Vector2<int> > s4;

My bad,
 
A

AG

Stack1 said:
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.

AG.
 
F

Frank Birbacher

Hi!
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::rebind<Node>::type".


Frank
 
B

Barry

Frank said:
Hi!


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::rebind<Node>::type".
Well, good example
but the /clumsy use/ of rebind is:

Allocator:: template rebind<Node>::eek:ther
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top