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).
QSharedPointerandQVectorandQMapetc throughout your code, throwing in a singlestd::unique_ptrmight throw a reader off if you could instead useQScopedPointer.