|
Boost : |
From: Gennadiy Rozental (rogeeff_at_[hidden])
Date: 2002-05-03 03:25:37
"Andrei Alexandrescu" <andrewalex_at_[hidden]> wrote in message
news:aaqd13$8sr$1_at_main.gmane.org...
[...]
>
> 1) A device that detects whether a class is empty or not. I believe
> that's
> already in boost's type_traits (is_empty).
>
> 2) A device that optionally inherits one, the other, or both of two
> classes depending whether the first, the second, or both are nonempty.
> Something like:
>
> template <class T, class U,
> bool forceInheritT = !type_traits<T>::is_empty,
> bool forceInheritU = !type_traits<U>::is_empty>
> struct OptionallyInherit : public T, public U
> {
> template <class V, class W>
> OptionallyInherit(const U& obj1, const W& obj2)
> : T(obj1), U(obj2)
> {
> }
> };
>
> // Inherit none; both are empty
> template <class T, class U>
> struct OptionallyInherit<T, U, false, false>
> {
> template <class V, class W>
> OptionallyInherit(const V&, const W&)
> {
> }
> };
>
>
> // Inherit U; T is empty
> template <class T, class U, >
> struct OptionallyInherit<T, U, false, true> : public U
> {
> template <class V, class W>
> OptionallyInherit(const V&, const W& obj2)
> : U(obj2)
> {
> }
> };
>
> // Inherit T; U is empty
> template <class T, class U, >
> struct OptionallyInherit<T, U, true, false> : public T
> {
> template <class V, class W>
> OptionallyInherit(const V& obj1, const W&)
> : T(obj1)
> {
> }
> };
>
> If I'm not mistaken, OptionallyInherit<T> as defined above "minimizes"
> inheritance in that it inherit either of its arguments iff that
> argument is nonempty. Many details can be refined. It is also possible
> that compressed_pair does exactly what OptionallyInherit does, which
> means we can use it out-of-the-box for policy packing!
>
> Now when defining smart_ptr, the policies come wrapped in
> OptionallyInherit:
>
> template
> <
> typename T,
> template <typename> class OwnershipPolicy,
> template <typename> class ConversionPolicy,
> template <typename> class CheckingPolicy,
> template <typename> class StoragePolicy
> >
> class smart_ptr
> : public OptionallyInherit<
> StoragePolicy<T>,
> OptionallyInherit<CheckingPolicy<
> typename StoragePolicy<T>::stored_type> >,
> OptionallyInherit<OwnershipPolicy<
> typename StoragePolicy<T>::pointer_type> >,
> OptionallyInherit<ConversionPolicy<
> typename StoragePolicy<T>::pointer_type> > > >
> {
> ...
> };
>
I was trying to implement this today. I did not finish but here list of
issues:
1. OptionallyInherit need default constructor
2. What to do with smart_ptr( stored_type const& )?
3. What to do with swap? Empty classes are not parents anymore and can't
convert argument. Was inforced to move swap into OptionallyInherit. Now
empty classes does not really need them.
4. As well as any other constructors. But this means that we lost
conertability checks. For example smart_ptr with disallow_convertion will
perfectly convert to allow conversion. The same this any other undersirable
conversion.
> Andrei
>
That'it for today, will try more tomorow.
Gennadiy.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk