Boost logo

Boost :

From: Roland Weiss (weissr_at_[hidden])
Date: 2001-06-26 11:18:24


On Tuesday 26 June 2001 17:45, you wrote:
> Jeremy Siek wrote:
> > Sure. The template template parameter workaround allows more
> > flexibility in the template types that are used as parameters. For
> > example, suppose the template parameter is a container:
> >
> > #include <vector>
> >
> > template <template <class U> class C>
> > struct foo {
> > struct bar { };
> > C<bar> c;
> > };
> >
> > int main()
> > {
> > foo<std::vector> f;
> > return 0;
> > }
> >
> >
> > Now, what if we don't want to use the default allocator of
> > std::vector, but some other allocator? How can we pass in std::vector
> > into
> >
> > foo but also specify some other allocator? With template templates
> > parameters you can not do this.
>
> Actually, with template template parameters you cannot even do the above
> ;),
>
> Comeau C/C++ 4.2.44 (Sep 30 2000 11:56:08) for _MS_WINDOWS_x86_BETA3
>
> "E:\depot\libs\template_params.cpp", line 12: error #999: class template
> "std::vector" is not compatible with template template parameter
> "C"
> foo<std::vector> f;
> ^

Yes, because a std container takes 2! template parameters: the type of the
contained elements and the allocator. You have to write it this way:

template <template <class, class> class C,
          template <class> A = std::allocator>
struct foo {
  struct bar { };
  C<bar, A<bar> >; // or some other allocator
  C<bar>; // illegal!
}

As pointed out in the other messages, what makes template template
parameters more rigid is the fixed number of parameters you have to
provide (the default parameter mechanism doesn't apply for ttps).

-- 
Roland

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk