Topic: Template template parameters (was Allocators to be removed from STL?)


Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/11/04
Raw View
>>>>> "MA" == Matt Austern <austern@sgi.com> writes:
[ I was asking why Allocator template parameters in standard containers
  were not template template parameters. -vdv ]
MA> Nathan Myers <ncm@cantrip.org> writes:
>> This issue arose in committee, and is interesting in general.
>> The question is best stated as, what-the-hell good are template template
>> parameters?
[...]
MA> So one other possible way to define this container adaptor would
MA> instead be
MA>  template <class T, template<class U> class Sequence = vector>
MA>  class stack { ... };.
MA> Using this definition, you would declare a stack of ints as
MA>  stack<int> S;
MA> and a stack of ints using a list as the underlying container as
MA>  stack<int, list> S;.
MA> That looks nicer, doesn't it?

Well, it's the same argument as what I used, but Nathan's argument seems
very persuasive: in this case, what if you wanted a special allocator
for your stack?

I suppose that is what you suggested in:
MA> [...] Third, there are actually some ways in which the present
MA> version is superior: it's a little bit more flexible, because it makes
MA> fewer assumptions about its underlying container.  Fourth, and most
MA> important, I haven't tested the version of stack that uses template
MA> template paremeters: I haven't found a compiler I could use to test
MA> that version.  Unless it's absolutely unavoidable, nothing should go
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
MA> into the standard unless it has been implemented and tested.  Untested
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
MA> designs are a big risk.

That is certainly a laudable philosophy, but it does not seem to be
one consistently followed by the C++ standardization committees.
Personally, I'm fairly happy with the approach taken: I believe
member templates, partial template specialization, explicit function
template designation and full-blown default template arguments were
not implemented when these features were added to the language.
However, they are some of the C++ language elements that I find
most interesting.

 Daveed


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Matt Austern <austern@sgi.com>
Date: 1996/10/31
Raw View
Nathan Myers <ncm@cantrip.org> writes:

> This issue arose in committee, and is interesting in general.
> The question is best stated as, what-the-hell good are template template
> parameters?

One place where template template parameters would be useful (or so
I conjecture: I haven't yet found a compiler I can use to test code
written with them) is in container adaptors.  For example, consider
the container adaptor "stack".

In Alex Stepanov's original version, the declaration of stack is
 template <class Sequence>
 class stack { ... };.
So, for example, if you wanted a stack of ints using a vector as the
underlying container, you would write
 stack<vector<int> > S;

Many people thought (correctly, in my opinion) that this looked
awkward and unnatural.  The declaration of stack in the working paper
is therefore different: the current declaration (23.2.3.3 in the
September '96 WP) is
 template <class T, class Sequence = deque<T> >
 class stack { ... };.
So if you want a stack of ints using the default deque as the
underlying container, you just write
 stack<int> S;.
This is better.  On the other hand, if you want to use a different
underlying container---which you might very well want to do, since
a vector is almost certainly faster than a deque---you would write
 stack<int, vector<int> > S;.
This is a bit unsettling: what would happen if you accidentally wrote
 stack<int, vector<double> > S;
instead?  The standard does actually say what would happen if you
wrote stack<int, vector<double> >; my point is just that it's a bit
unfortunate that you even have to ask that question.

So one other possible way to define this container adaptor would
instead be
 template <class T, template<class U> class Sequence = vector>
 class stack { ... };.
Using this definition, you would declare a stack of ints as
 stack<int> S;
and a stack of ints using a list as the underlying container as
 stack<int, list> S;.
That looks nicer, doesn't it?

Please note that I'm using this as an example where template template
parameters are useful: I'm definitely not suggesting that stack should
be changed.  There are four good arguments against changing stack at
this point.  First, the current definition isn't broken, and it's too
late to make changes in the standard except to fix things that are
broken.  Second, the improvement (if it really is an improvement) is
minimal.  Third, there are actually some ways in which the present
version is superior: it's a little bit more flexible, because it makes
fewer assumptions about its underlying container.  Fourth, and most
important, I haven't tested the version of stack that uses template
template paremeters: I haven't found a compiler I could use to test
that version.  Unless it's absolutely unavoidable, nothing should go
into the standard unless it has been implemented and tested.  Untested
designs are a big risk.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]