template template classes

8 views
Skip to first unread message

Anonymous

unread,
Sep 3, 2007, 6:23:26 AM9/3/07
to
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

Message has been deleted

Barry

unread,
Sep 3, 2007, 6:50:36 AM9/3/07
to
Anonymous wrote:
> 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.

> 2). A simple example
>
> I have read various articles etc, but it still dosen't seem to make
> sense to me
>
>
>


--
Thanks
Barry

Barry

unread,
Sep 3, 2007, 6:53:45 AM9/3/07
to
Barry wrote:
> Anonymous wrote:
>> 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;

My bad,
Stack2<int, Vector2> s4;

AG

unread,
Sep 4, 2007, 3:29:05 AM9/4/07
to
> Stack1<int, Vector2<int> > s2;

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

Frank Birbacher

unread,
Sep 4, 2007, 4:31:19 AM9/4/07
to
Hi!

Anonymous schrieb:


> 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

Barry

unread,
Sep 4, 2007, 5:02:52 AM9/4/07
to
Well, good example
but the /clumsy use/ of rebind is:

Allocator:: template rebind<Node>::other


--
Thanks
Barry

Frank Birbacher

unread,
Sep 4, 2007, 7:50:01 PM9/4/07
to
Barry schrieb:
> Well, good example

Thanks! :D

> but the /clumsy use/ of rebind is:
>
> Allocator:: template rebind<Node>::other

Well, even worse. But seems you got the point. ;)

Frank

Reply all
Reply to author
Forward
0 new messages