Boost logo

Boost :

From: Beman Dawes (beman_at_[hidden])
Date: 1998-12-07 16:44:10


At 06:30 PM 12/7/98 +0100, Kühl, Dietmar wrote:

>> Yes, it would be nice to be able to write:
>>
>> shared_ptr<MyClass> my_default_ptr; // direct_shared_ptr
>> shared_ptr<MyClass, indirect> my_indirect_shared_ptr;
>> shared_ptr<MyClass, embedded> my_embedded_shared_ptr;
>>
>This is easy to achieve! Here is how:
>
> // types used to choose the reference counting method:
> struct direct {};
> struct indirected {};
> template <typename T, long T::*count> struct embedded {};
>
> // declaration of the actual class which is then specialized
> // for the different methods:
> template <typename T, typename method = direct>
> class shared_ptr;
>
> // finally the various definitions. this is where the interface
> // for the classes is defined. This might involve some
> // redundant typing. However, since the methods are quite
> // different, I think there is only few code to be shared.
> template <typename T>
> class shared_ptr<T, direct> { ... };
> template <typename T>
> class shared_ptr<T, indirect> { ... };
> template <typename T, long T::*count>
> class shared_ptr<T, embedded<T, count> > { ... };
>
>That is, the class 'share_ptr' is simply specialized over the
>supported methods. For the methods 'direct' and 'indirect'
>it is sufficient to have just some types to identify the method.
>For the embedded method I think it is reasonable to have a
>way how the user can choose the name of the member. This
>is done with the template arguments for the embedded
>method.

This seems a neat approach, better than the forwarding class approach
I tried and gave up on.

But when I tried your approach with real-world compilers, there were
a couple of problems. I don't know if they are killers or not.

First, it requires a compiler which supports partial specialization.
So it doesn't work for the MS VC++ 5.0 compiler I use. Perhaps
someone could say if version 6.0 solves this one.

Second, I couldn't get the embedded count approach to work with the
Intel/EDG 3.0 compiler, although that doesn't seem critial to me. I
don't have any problem requiring the specific name (say "use_count")
for the embedded counter. Bye the way, I would not specify it as a
long, but rather just specify the two or three expressions which must
be well formed.

--Beman
------------------------------------------------------------------------
Free Web-based e-mail groups -- http://www.eGroups.com


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