|
Boost : |
From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-06-26 10:32:38
On Tue, 26 Jun 2001, Beman Dawes wrote:
bdawes>
bdawes> But in another message someone claimed that template
bdawes> template parameter replacements were not just broken
bdawes> compiler workarounds, but actually better that template
bdawes> template parameters. Could someone explain the rationale
bdawes> behind that assertion?
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. However, with the workaround it is easy:
#include <vector>
template <class Tag> struct generate_container { };
template <class C_tag>
struct foo {
struct bar { };
typename generate_container<C_tag>::template bind<bar>::type c;
};
struct vec_tag { };
template<> struct generate_container<vec_tag> {
template <class T> struct bind {
typedef std::vector<T, std::allocator<T> > type;
};
};
int main()
{
foo<vec_tag> f;
return 0;
}
Cheers,
Jeremy
----------------------------------------------------------------------
Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
Ph.D. Candidate, IU B'ton email: jsiek_at_[hidden]
Summer Manager, AT&T Research phone: (973) 360-8185
----------------------------------------------------------------------
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk