I recently did a short tongue-in-cheek blog post about Qt and modern C++. In the comments, people discovered that several compilers effectively can optimize
std::make_unique<>().release() to a simple
new statement, which was kind of a surprise to me.
I have recently written a new program from scratch (more about that later), and I tried to force myself to use standard library smartpointers much more than what I normally have been doing.
I ended up trying to apply a set of rules for memory handling to my code base to try to see where it could end.
newstatements, unless it was handed directly to a Qt function taking ownership of the pointer. (To avoid sillyness like the previous one)
- Raw pointers in the code are observer pointers. We can do this in new code, but in older code it is hard to argue that.
It resulted in code like
m_document = std::make_unique<QTextDocument>();
auto layout = std::make_unique<QHBoxLayout>();
auto textView = std::make_unique<QTextBrowser>();
By it self, it is quite ok to work with, and we get all ownership transfers documented. So maybe we should start code this way.
But there is also a hole in the ownership pass around, but given Qt methods doesn’t throw, it shouldn’t be much of a problem.
More about my new fancy / boring application at a later point.
I still haven’t fully embraced the c++17 thingies. My mental baseline is kind of the compiler in Debian Stable.