[Interest] QExplictlySharedDataPointer vs QSharedPointer

Thiago Macieira thiago.macieira at intel.com
Thu Sep 8 20:29:39 CEST 2016


On quinta-feira, 8 de setembro de 2016 18:50:48 PDT Elvis Stansvik wrote:
> QExplicitlySharedDataPointer (QESDP) is a cousin of QISDP with the
> important difference that you have to explicitly call detach() to
> detach from the underlying data.
> 
> QSharedPointer is Qt's own "smart pointer" to shared data, similar to
> std::shared_ptr.
> 
> I'm curious about the role/use for QESDP compared to QSharedPointer.
> The main differences I can see is
> 
>  * QESDP requires that the pointed-at object derive from QSharedData.
>  * QSharedPointer provide no easy means to detach/make copies of the
> underlying object

That's the difference in what they're sharing: QSharedPointer shares a 
*pointer*, whereas QSharedData shares data (QSharedDataPointer points to the 
QSharedData). QESDP doesn't actually require that you derive from QSharedData, 
only that you have an atomic integer called "ref" in your structure.

If you want to make copies of the object QSharedPointer holds, just do it like 
you've always done in C++. Or to create a new QSP:

	auto copy = QSharedPointer::create<MyClass>(origin.constData());

Do you want a convenience function? It's easy to add it, so please submit your 
contribution.

> But regarding this second difference, wouldn't it be easy enough to
> just take the raw pointer to the underlying object with data(), and
> make a copy of that?

Yes.

> In short, I fail to see a strong use case for QESDP compared to using
> a QSharedPointer. I'm probably missing something.

The fact that it's a smart pointer, not a raw one.

> Does anyone know of a code base that makes liberal use of QESDP, or
> could you share how you're making use of it? It's easy to find uses
> for the implicit variant (QISDP).

qtxmlpatterns, I believe.

> In general I'm curious to hear advise about the do's and don'ts when
> it comes to QISDP, QESDP and QSharedPointer when designing the
> innermost parts (data model) of a Qt application. I've heard some
> warning words about using QISDP too library, because of the risks of
> "accidental detach".

QSharedPointer is std::shared_ptr; QSDP and QESDP are (non-existent, but in 
discussion) std::intrusive_ptr, with the former doing implicit detaching.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list