32

So when programming in Qt I like to go with the Qt implementation as far as possible. As far as I've seen there's no Qt version of the std::unique_ptr or is there?

What would be the alternatives that produce the "same" result in Qt if it doesn't exist?

7
  • Why do you specifically want a Qt version? Are you trying to write C++ without a standard library? Commented Sep 11, 2014 at 11:54
  • 2
    @MikeSeymour not particularly but there's no need for mixing the std with Qt when the Qt implementation covers it all :) Commented Sep 11, 2014 at 11:59
  • 4
    @deW1 : That's not how it works, the STL is something you should use every time when it solve your problem. It's just so well written that any other library can't be as effective as STL is. I suggest you to only use Qt implementation for things, that doesn't have a solution already in the STL. Commented Sep 11, 2014 at 12:03
  • 2
    To counter the points about using the std library/STL, I'd argue that consistency increases readability, which is important for maintaining code. So if you're using QSharedPointer and QVector and QMap etc throughout your code, throwing in a single std::unique_ptr might throw a reader off if you could instead use QScopedPointer. Commented Feb 25, 2018 at 0:18
  • 2
    I feel that this misses the point of why QT containers continue to exist and were not obsoleted by the STL. QT-based Objects cannot be copied or moved as their operators are deleted by design so they need specialized containers. Part of this is to ensure that signals and slots targeting a QObject can still function however, the restriction makes some of the containers a little less than optimal. It's my opinion that you should use the right class or structure for the job instead of being uniform just for the sake of being uniform. Commented Aug 24, 2021 at 23:08

2 Answers 2

38

The Qt equivalent to std::unique_ptr is QScopedPointer.

It's significantly less useful as it doesn't support move semantics, making it closer in utility to C++03 std::auto_ptr (Why is auto_ptr being deprecated?).

Sign up to request clarification or add additional context in comments.

4 Comments

QScopedPointer isn't broken like auto_ptr is it? It doesn't transfer ownership on copy and assignment, it just doesn't support copy and assignment. (afaict, reading the documentation)
@Benjamin Lindley indeed, it doesn't have a copy constructor at all, not even a non-const one.
TBH it does have custom deleters, which auto_ptr didn't have.
QScopedPointer<T> is probably closer to const std::unique_ptr<T> than to anything else.
0

The std::unique_ptr and the now deprecated std::auto_ptr, can be used to "force" that the pointed-value is not changed after return.

Like in one of my projects, our library provided an Interface, where we wanted to restrict user-implemented version of said interface, to ensure they don't accidentally change MyType's value later (after return), like:

struct MyType { /* ... */ };

typedef std::unique_ptr<MyType> MyTypeUniquePtr;

class MyLibInterface {
public:
    virtual ~MyLibInterface();

    virtual MyTypeUniquePtr doSomething() = 0;
};

If said "return" or restriction is NOT needed by your code, then the QScopedPointer class can be used to ensure resources get released once scope ends, like if throw happens.

But that's it, QScopedPointer uses Q_DISABLE_COPY, hence move-constructor gets forbidden as well (if compiler is sane), and return should cause compile error.

There is no class fully-equivalent to std::unique_ptr in original Qt, hence if your target tool-chains/ compilers all support std::unique_ptr, simply use that, however, I may add QUniquePointer to my XD framework, just to support more compilers (more, not all).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.