On Friday, 23 February 2018 23:16:52 UTC+2, Vir Campestris wrote:
> I can't see one - but is there any way to distinguish an expired
> weak_ptr (which normally refers to a control block with a zero strong
> reference count) and one that has never been set (not referring to any
> control block)?
There are no standard ways. That leaves implementation-specific hacks.
Even such may be insufficient since you describe only two ways to reach
empty weak_ptr but in reality there are more. In most implementations
a weak_ptr that has been .reset() is impossible to distinguish from one
that has never been set or from one that was constructed from empty
shared_ptr.
> I'd like to distinguish them. The latter case indicates a logic error in
> our code.
Why? It is cheaper to make a class to enforce object's invariant
when its state changes than to do sporadic checks of states of
objects later and then to try to deal with objects in incorrect
state. So enclosing class can refuse to (or make unavailable to)
defaut-construct that weak_ptr or assign or construct it from
empty weak_ptr or empty shared_ptr or to reset it.