31

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?

deW1
  • 5,562
  • 10
  • 38
  • 54
  • Why do you specifically want a Qt version? Are you trying to write C++ without a standard library? – Mike Seymour Sep 11 '14 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 :) – deW1 Sep 11 '14 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. – Melkon Sep 11 '14 at 12:03
  • *"It's just so well written that any other library can't be as effective as STL is."* -- Err, that's a bit of a stretch. Although I would agree that it does most things as good or better than Qt (at least for my purposes), for those things which both Qt and the standard library do. – Benjamin Lindley Sep 11 '14 at 12:07
  • Well, i wrote it as a general rule, there can be some exception, but i believe that trying to avoid STL doesn't make much sense. – Melkon Sep 11 '14 at 12:09
  • 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`. – Jamie S Feb 25 '18 at 00:18
  • 1
    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. – Klypto Aug 24 '21 at 23:08

1 Answers1

37

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?).

ecatmur
  • 152,476
  • 27
  • 293
  • 366