[smart_ptr] shared_from_this

Hello again, does shared_from_this() also guarantee that it never throws? Regards, Daniel

Daniel Frey wrote:
Hello again,
does shared_from_this() also guarantee that it never throws?
I think sharead_from_this() can not make this guarantee since it uses BOOST_ASSERT() which can be defined to call a user-supplied assertion handler. That handler could, of course, throw. Kevin

On Wed, 2008-03-26 at 09:07 +0100, Kevin Scarr wrote:
Daniel Frey wrote:
does shared_from_this() also guarantee that it never throws?
I think sharead_from_this() can not make this guarantee since it uses BOOST_ASSERT() which can be defined to call a user-supplied assertion handler. That handler could, of course, throw.
Yepp, but I think it's not relevant. Does it guarantee to never throw when used correctly? A lot of exception guarantees expect dtor's to never throw, although in theory they could. asserts (which also includes BOOST_ASSERT) are meant to be never triggered when used correctly, right? So a guarantee like "never throws" always assumes correct usage. Also, my question is not about the current *implementation* (or any specific implementation at all), but about the *interface* shared_ptrs intends to provide. What happens if I switch to GCC's implementation of std::shared_ptr in the future? Or some other compiler/platform? Can I rely on certain functions to never throw? I just think that this needs to be documented clearly. Regards, Daniel

Daniel Frey wrote:
Yepp, but I think it's not relevant. Does it guarantee to never throw when used correctly? A lot of exception guarantees expect dtor's to never throw, although in theory they could. asserts (which also includes BOOST_ASSERT) are meant to be never triggered when used correctly, right? So a guarantee like "never throws" always assumes correct usage.
Ok. I take the approach "never throws" actually means "never throws" regardless of usage.
Also, my question is not about the current *implementation* (or any specific implementation at all), but about the *interface* shared_ptrs intends to provide. What happens if I switch to GCC's implementation of std::shared_ptr in the future? Or some other compiler/platform?
Sorry, I thought you were asking about boost::shared_ptr. I did not know that a std::shared_ptr had been defined. If it has, then you would need to look to its documentation. Kevin

On Wed, 2008-03-26 at 11:56 +0100, Kevin Scarr wrote:
Daniel Frey wrote:
Also, my question is not about the current *implementation* (or any specific implementation at all), but about the *interface* shared_ptrs intends to provide. What happens if I switch to GCC's implementation of std::shared_ptr in the future? Or some other compiler/platform?
Sorry, I thought you were asking about boost::shared_ptr. I did not know that a std::shared_ptr had been defined. If it has, then you would need to look to its documentation.
Erm, OK, it's not yet defined, but I think this is the intention, Peter explicitly working on it and IIUC he already "donated" boost::shared_ptr's code to the GCC people. Maybe my formulation wasn't very good. I hope it's OK if I address these issues here, with the boost implementation only as an example (or reference implementation) but also looking at the larger picture of intended behavior and guarantees of what should be standardized in the future. Regards, Daniel

On Wed, 2008-03-26 at 07:38 -0400, Frank Mori Hess wrote:
On Wednesday 26 March 2008 02:55, Daniel Frey wrote:
Hello again,
does shared_from_this() also guarantee that it never throws?
No, it can throw bad_weak_ptr.
<http://boost.org/libs/smart_ptr/smart_ptr.htm> states that only std::bad_alloc is thrown - if anything is thrown at all. Am I misreading that? And reading the documentation for weak_ptr, it's not clear to me when a bad_weak_ptr exception is thrown at all. All weak_ptr functions are documented to never throw. Regards, Daniel BTW: In the introduction, it says that there are five smart pointer classes, then it lists six... :)

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Wednesday 26 March 2008 08:00 am, Daniel Frey wrote:
On Wed, 2008-03-26 at 07:38 -0400, Frank Mori Hess wrote:
On Wednesday 26 March 2008 02:55, Daniel Frey wrote:
Hello again,
does shared_from_this() also guarantee that it never throws?
No, it can throw bad_weak_ptr.
<http://boost.org/libs/smart_ptr/smart_ptr.htm> states that only std::bad_alloc is thrown - if anything is thrown at all. Am I misreading that?
That's talking about the smart pointer classes. shared_from_this is a method of enable_shared_from_this, which isn't a smart pointer.
And reading the documentation for weak_ptr, it's not clear to me when a bad_weak_ptr exception is thrown at all. All weak_ptr functions are documented to never throw.
It's thrown by the shared_ptr constructor that takes a weak_ptr argument. - -- Frank -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFH6k8R5vihyNWuA4URAsOqAKDA20XnxM+S95VOGXcr7t0JhwDfNwCghUZA 2EODtZcRe3M8JrOXgFNHVkE= =70v9 -----END PGP SIGNATURE-----

On Wed, 2008-03-26 at 09:26 -0400, Frank Mori Hess wrote:
does shared_from_this() also guarantee that it never throws?
No, it can throw bad_weak_ptr.
<http://boost.org/libs/smart_ptr/smart_ptr.htm> states that only std::bad_alloc is thrown - if anything is thrown at all. Am I misreading that?
That's talking about the smart pointer classes. shared_from_this is a method of enable_shared_from_this, which isn't a smart pointer.
See last paragraph.
And reading the documentation for weak_ptr, it's not clear to me when a bad_weak_ptr exception is thrown at all. All weak_ptr functions are documented to never throw.
It's thrown by the shared_ptr constructor that takes a weak_ptr argument.
But shared_from_this() requires that there must exist at least one shared_ptr instance that owns the object, which means that the use_count() is always guaranteed to be >0, hence the exception is never thrown by shared_ptr's ctor. Oh, and the ctor of shared_ptr therefore *can* throw something else than std::bad_alloc, right? Unlike shared_from_this(), AFAICS. Regards, Daniel

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Wednesday 26 March 2008 11:08 am, Daniel Frey wrote:
But shared_from_this() requires that there must exist at least one shared_ptr instance that owns the object, which means that the use_count() is always guaranteed to be >0, hence the exception is never thrown by shared_ptr's ctor.
The exception is thrown if the user violates the requirement. Actually, on the svn trunk the requirement is looser, shared_from_this can be called before a shared_ptr has taken ownership (the docs haven't been updated to reflect that though). It's still possible to throw though, if the object outlives the shared_ptrs that own it.
Oh, and the ctor of shared_ptr therefore *can* throw something else than std::bad_alloc, right?
Yes, and it says so in the constructor's documentation. I'm not familiar with the precise rules of C++ language specification-ese, so I don't know if the blanket statement about the classes' "functions" in the intro also applies to constructors? - -- Frank -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFH6mqi5vihyNWuA4URAvuhAKDRAlY6DMuwkB7C2Toq1VKxdiKWvwCfake0 IMG6Vdh17iadvwY3Rt18KFs= =REta -----END PGP SIGNATURE-----
participants (4)
-
Daniel Frey
-
Frank Mori Hess
-
Frank Mori Hess
-
Kevin Scarr