[Smart_Ptr] Error in intrusive_ptr ctors' exception specification?
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
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.
On 3/6/06, Peter Dimov
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
participants (3)
-
Daniel James
-
Peter Dimov
-
Scott Meyers