Have you ever hoped that C++ would have digit separators? That you wouldn’t have to strain your eyes when reading 2147483647 (is it std::numeric_limits<int32_t>::max(), or is it just similar)? That you wouldn’t have to count the zeros 5 times when typing 1000000000?
Well, the C++ Standards Committee doesn’t have your back. Oh, sure, they have introduced a digit separator, – ‘, but it’s completely unusable in production code! Here’s why.
I’ve been a fan of Fabien Sanglard’s website and, more accurately, his phenomenal code reviews of older games for quite some time now – if you don’t know what I mean, follow the link above and check for yourself. Naturally, I was very excited to learn about him writing a book. So much, in fact, that I considered pre-ordering it, which goes against my core beliefs. In the end, I didn’t pre-order1, but that was only because I wanted a signed copy (and I got one much more personalized than I dared to hope for).
I came upon a similar piece of code during an IRC discussion. While I am certain that some may consider this example trivial, I admit that the correct answer eluded me even after I verified the result with the compiler – it wasn’t until I checked the standard that it became clear.
With the increasing probability of your average project using at least C++11, std::unique_ptr is one of the most popular solutions1 to dynamically managing memory in C++.
It usually goes like this: if your object can’t be a scoped object, that is one with automatic lifetime (colloquially: on the stack, or a class’s member), wrap it in std::unique_ptr. This will ensure it’s freed when it finally goes out of scope. Herb Sutter goes more in-depth into this topic in his last C++Con talk:
Just a quick note: I’ve written an article outlining features of the upcoming C++17. It’s available in the freshly published issue of the “Programista” magazine (available in subscription, Empik stores and probably somewhere else, too). If you can read in Polish you might find it interesting :)