[Development] Making QScopedPointer scoped (again)

Olivier Goffart olivier at woboq.com
Wed Sep 4 07:36:43 CEST 2013


On Tuesday 03 September 2013 21:20:20 Peter Kümmel wrote:
> It is of great benefit that you never had to think about if
> QScopedPointer(5.1) will delete when leaving scope.

That's not true.

 QScopedPointer<...> myPtr(foo());
 myPtr.take();

And it does not delete when leaving the scope.

Think of qMove(myPtr) of a safer convenience to the already existing take()

> The main point is that in Qt is nothing like std::unique_ptr,

False again. QScopedPointer is almost exactly the same as unique_ptr. They 
have the same API.
Compare http://en.cppreference.com/w/cpp/memory/unique_ptr and http://qt-project.org/doc/qt-5.0/qtcore/qscopedpointer.html and tell me again they are 
nothing like eachother.

The documentation of unique_ptr even starts with: "std::unique_ptr is a smart 
pointer that retains sole ownership of an object through a pointer and 
destroys that object when the unique_ptr goes out of scope."

It is unfortunate that the standard did not choose the name 
std::scoped_pointer,  but that's not the first time the standard and Qt are 
using different name (QByteArray <> std::string, QLinkedList <> std::list, ...)

(I assume you meant s/Qt/QScopedPointer/)

> so when it is needed and we can't use std::unique_ptr, we have to introduce
> a QUniquePointer. This is better than to add features to classes which are
> contradictorily to their naming.

Based on all the false assumptions in your mail, I think it's safe to discard 
your conclusion.

-- 
Olivier

Woboq - Qt services and support - http://woboq.com - http://code.woboq.org





More information about the Development mailing list