intrusive_ptr feature request for constructor
Hi. intrusive_ptr's constructor takes an bool to signal whether the refcount should be initially incremented or not. The default is true. I find that the default is different for different types, so I propose to add a hook to specify the policy for each type. At first I thought to do this with traits, but now I think a function is better because this doesn't need to be compile-time and a function overload works fine with ADL. I'm no expert in this, so maybe I'm mistaken somewhere. What I propose is to add: namespace boost { template <class T> bool intrusive_ptr_add_ref_at_construction(T*) { return true; } template<class T> class intrusive_ptr { intrusive_ptr(T * p, bool add_ref = intrusive_ptr_add_ref_at_construction((T*)0)): p_(p) { if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_); } ... } } then I can do bool intrusive_ptr_add_ref_at_construction(MyType*) { return false; } What do you think of this feature? Regards, Bruno
Bruno Martínez wrote:
Hi.
intrusive_ptr's constructor takes an bool to signal whether the refcount should be initially incremented or not. The default is true.
I find that the default is different for different types, [...]
No, it shouldn't be. I suspect that you have in mind a type whose initial reference count is one instead of zero. This type is incorrectly designed. Here's why: T * p = new T; intrusive_ptr<T> p1( p ); intrusive_ptr<T> p2( p ); Note that, in order to be able to reliably create an intrusive_ptr from a raw pointer (a unique selling point for intrusive counting,) the initial reference count needs to be zero.
On Sun, 13 Nov 2005 17:17:27 -0200, Peter Dimov
Bruno Martínez wrote:
Hi.
intrusive_ptr's constructor takes an bool to signal whether the refcount should be initially incremented or not. The default is true.
I find that the default is different for different types, [...]
No, it shouldn't be. I suspect that you have in mind a type whose initial reference count is one instead of zero.
You are right.
This type is incorrectly designed. Here's why:
T * p = new T; intrusive_ptr<T> p1( p ); intrusive_ptr<T> p2( p );
I agree. There's nothing I can do to change the type, tough.
Note that, in order to be able to reliably create an intrusive_ptr from a raw pointer (a unique selling point for intrusive counting,) the initial reference count needs to be zero.
In my case, I never use that feature. All my intrusive_ptr are constructed with a false second argument. I wasn't aware that intrusive_ptr's constructor was implicit, though. It seems thing should be left as they are now. Thanks, Bruno
participants (2)
-
Bruno Martínez
-
Peter Dimov