What preconditions does enable_shared_from_this impose, and when is weak_from_this safer for callbacks?
Show answer and explanation
Publicly and unambiguously inheriting enable_shared_from_this lets a shared_ptr that first owns the object initialize an internal weak reference to that same control block. Calling shared_from_this before such ownership exists throws bad_weak_ptr, and constructing another shared_ptr directly from this still creates a separate control block and risks double deletion. A callback can capture weak_from_this, lock it when invoked, and do nothing after expiration; this avoids both a dangling raw this and an ownership cycle or unexpectedly prolonged lifetime caused by capturing a shared_ptr.
Publicly and unambiguously inheriting enable_shared_from_this lets a shared_ptr that first owns the object initialize an internal weak reference to that same control block. Calling shared_from_this before such ownership exists throws bad_weak_ptr, and constructing another shared_ptr directly from this still creates a separate control block and risks double deletion. A callback can capture weak_from_this, lock it when invoked, and do nothing after expiration; this avoids both a dangling raw this and an ownership cycle or unexpectedly prolonged lifetime caused by capturing a shared_ptr.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.