[Smart_Ptr] Error in intrusive_ptr ctors' exception specification?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Smart_Ptr] Error in intrusive_ptr ctors' exception specification?

Scott Meyers
Consider this constructor from the intrusive_ptr doc
(http://boost.org/libs/smart_ptr/intrusive_ptr.html):

   intrusive_ptr(T * p, bool add_ref = true);

   Effects: if(p != 0 && add_ref) intrusive_ptr_add_ref(p);.

Note in particular that nothing is said about exceptions being thrown.
Presumably intrusive_ptr_add_ref may throw, as I can't find any restriction
to the contrary.

Now consider these constructors from the same document:

   intrusive_ptr(intrusive_ptr const & r);                      // never throws
   template<class Y> intrusive_ptr(intrusive_ptr<Y> const & r); // never throws

   Effects: if(r.get() != 0) intrusive_ptr_add_ref(r.get());.

They are documented not to throw, yet both call intrusive_ptr_add_ref.  How
can it be that the top constructor above may throw, but these constructors
may not?

FWIW, it seems to me that all three should be allowed to throw, unless some
constraint is imposed on the implementation of intrusive_ptr_add_ref.  For
example, perhaps intrusive_ptr_add_ref wants to throw if its reference
count will overflow -- maybe because it's using only an 8-bit counter.
Isn't that possible?

Scott

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Smart_Ptr] Error in intrusive_ptr ctors' exceptionspecification?

Peter Dimov
Scott Meyers wrote:

> Consider this constructor from the intrusive_ptr doc
> (http://boost.org/libs/smart_ptr/intrusive_ptr.html):
>
>   intrusive_ptr(T * p, bool add_ref = true);
>
>   Effects: if(p != 0 && add_ref) intrusive_ptr_add_ref(p);.
>
> Note in particular that nothing is said about exceptions being thrown.
> Presumably intrusive_ptr_add_ref may throw, as I can't find any
> restriction to the contrary.
>
> Now consider these constructors from the same document:
>
>   intrusive_ptr(intrusive_ptr const & r);  // never throws
>   template<class Y> intrusive_ptr(intrusive_ptr<Y> const & r); // never
> throws
>
>   Effects: if(r.get() != 0) intrusive_ptr_add_ref(r.get());.
>
> They are documented not to throw, yet both call
> intrusive_ptr_add_ref.  How can it be that the top constructor above
> may throw, but these constructors may not?

You are right, this is a documentation bug (similarly for operator=). The
"// never throws" annotations aren't present in the synopsis. Fixed in CVS,
thanks for the report.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Smart_Ptr] Error in intrusive_ptr ctors' exceptionspecification?

Daniel James
On 3/6/06, Peter Dimov <[hidden email]> wrote:
> Scott Meyers wrote:
> > They are documented not to throw, yet both call
> > intrusive_ptr_add_ref.  How can it be that the top constructor above
> > may throw, but these constructors may not?
>
> You are right, this is a documentation bug (similarly for operator=). The
> "// never throws" annotations aren't present in the synopsis. Fixed in CVS,
> thanks for the report.

It might be a good idea to add a note about how intrusive_ptr_add_ref
is expected to behave if it does throw. It seems to be implied that
when an exception is thrown, it shouldn't change the reference count,
and should destroy the object if the reference count is zero, but it's
quite subtle.

Daniel

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users