Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Varieties of expired weak_ptr

36 views
Skip to first unread message

Vir Campestris

unread,
Feb 23, 2018, 4:16:52 PM2/23/18
to
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)?

I'd like to distinguish them. The latter case indicates a logic error in
our code.

Andy

Alf P. Steinbach

unread,
Feb 23, 2018, 5:28:02 PM2/23/18
to
You can wrap it in a class that doesn't have default constructor.

Static check better than dynamic. ;-)


Cheers!,

- Alf


Öö Tiib

unread,
Feb 24, 2018, 8:44:42 AM2/24/18
to
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.

Vir Campestris

unread,
Feb 26, 2018, 4:56:58 PM2/26/18
to
On 23/02/2018 22:27, Alf P. Steinbach wrote:
>
> You can wrap it in a class that doesn't have default constructor.
>
> Static check better than dynamic. ;-)

That might do it. I'll have a play next time I have some free time.

Fixing core bugs in shared code isn't my real job - I'm working on a
specific product - but I do seem to have found a few over the months
I've been here.

Thanks Alft & Öö.

Andy
0 new messages