|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2000-09-16 07:52:31
----- Original Message -----
From: <jsiek_at_[hidden]>
>
> Template templates are never really needed, since they
> can be replaced by using partial specialization. Instead
> of having two versions of the code, one with template templates
> and one with the partial spec. version, I think its better
> to just maintain the one version that is more portable,
> the partial spec. version. Also, the partial spec can
> be replaced with something even more portable, but that
> starts to get really messy...
>
>
> with template templates:
>
> template<class U, template <class T> Container>
> struct bar {
> typedef Container<U> foo;
> ...
> };
>
> bar<int, std::vector> b;
>
> with partial spec:
>
> template <class ContainerTag, class U>
> struct container_generator { };
>
> template <class U, class ContainerTag>
> struct bar {
> typedef typename container_generator<ContainerTag, U>::type fooX;
> ...
> };
>
> struct vector_tag { };
>
> template <class T>
> struct container_generator<vector_tag, T> {
> typedef std::vector<T> type;
> };
>
> bar<int, vector_tag> b;
Actually, this is a perfect candidate for "poor man's partial
specialization", as I like to call it, and IMO not too messy. OTOH, I am
forced to use VC++, so of course I think it's worth it ;)
withOUT partial spec:
template <class ContainerTag>
struct container_ {
template <class U>
struct generator {
typedef void type;
};
};
template <class U, class ContainerTag>
struct bar {
typedef typename container_<ContainerTag>::generator<U>::type fooX;
...
};
struct vector_tag { };
template <>
struct container_<vector_tag> {
template <class T>
struct generator {
typedef std::vector<T> type;
}
};
bar<int, vector_tag> b;
------
if you wanted to keep container_generator<C, U> as an available syntax, you
could always add:
template <class C, class U>
struct container_generator : container_<C>::generator<U> {};
"Poor man's partial spec." can be used any time you need to specialize on a
match of some, but not all, of the template parameters. It is useless for
such important things as specializing an algorithm for all pointer types,
const references, arrays, etc.
-Dave
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk