C++ Logo


Advanced search

Re: [std-proposals] std::shared_ptr resurrect

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 11 Dec 2023 07:57:40 -0800
On Monday, 11 December 2023 07:46:00 PST Valentin Palade via Std-Proposals
> std::shared_ptr<T> sm2(std::move(sm));
> will preserve the use count in the control block attached to the object
> referenced by sm so the if (sm2.use_count()) will be similar to if
> (sm.use_count()).
> The idea is that the validation that we are the last instance referencing a
> certain object, must be made atomically

This is an equivalent check, but more expensive. Jonathan's suggestion is
faster and has the benefit of working since C++11, with no additional API.

I would even argue that you should already have a local stack object in the
first place in your design, not referring to some globally-visible
std::shared_ptr variable. You won't need to add more code.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-12-11 15:57:42