|
Boost : |
From: David B. Held (dheld_at_[hidden])
Date: 2002-10-02 23:54:34
If anyone needs to be convinced that template templates are a good and
useful feature of C++, let them take a look at this code, which is an
adaptation of shared_static_cast for policy-based pointers:
template <
typename T,
BOOST_SMART_POINTER_PARAMETERS,
typename U
>
inline smart_ptr<T,
typename OwnershipPolicy::template rebind<
typename StoragePolicy::template rebind<T>::other::pointer_type
>::other,
typename ConversionPolicy::template rebind<
typename StoragePolicy::template rebind<T>::other::pointer_type
>::other,
typename CheckingPolicy::template rebind<
typename StoragePolicy::template rebind<T>::other::stored_type
>::other,
typename StoragePolicy::template rebind<T>::other
>
smart_static_cast(
smart_ptr<U, BOOST_SMART_POINTER_POLICIES> const& p
)
{
return smart_ptr<T,
typename OwnershipPolicy::template rebind<
typename StoragePolicy::template
rebind<T>::other::pointer_type
>::other,
typename ConversionPolicy::template rebind<
typename StoragePolicy::template
rebind<T>::other::pointer_type
>::other,
typename CheckingPolicy::template rebind<
typename StoragePolicy::template
rebind<T>::other::stored_type
>::other,
typename StoragePolicy::template rebind<T>::other
>(
p, detail::static_cast_tag()
);
}
Note also that this code is somewhat simplified by macros I've been
using, and that this technique requires *every policy* to define a rebind<>
template. Also keep in mind that there are three other casts like this. :(
What a high price to pay for lack of template templates!!
I'm trying to figure out a way to avoid defining a rebind<> template
for every policy. I'm thinking of something like this:
template <class T>
struct rebindable
{
template <typename U>
struct rebind
{ typedef T<U> other; };
};
Then maybe you could do:
template <typename P>
struct policy : public rebindable<policy>
{
...
};
If anyone has any clever ideas, or ways I can avoid rebind entirely in
the code above, I'm open to suggestions.
Dave
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk