Smart Pointers and Memory Ownership in C++ Libraries
I don’t often get deep into technical minutiae on this blog, preferring to write for a more general audience. Despite that, programming computers is still my day job, and the articles that do get into technical detail tend to get people interested: the article that dove deep into the type system I developed for my thesis got some recent attention from Hacker News, the article about how to extract messages from WeChat is consistently one of the pages with the most hits (presumably because it’s useful to some people), and I’ve even gotten some good feedback and attention to my unpolished and unedited programming languages notes from university, which I only barely ever publicized.
@Kronopath I've learnt more Scala in 20 minutes of reading your notes for April 27/16 than in all last week. Your style is great!
— Francesco Dondi (@fdondi1) March 10, 2017
I’ve even gotten a bit of a reputation at work as someone who can explain things to people in a way that’s understandable. I’ve been trying to lean into that strength at my job, and while I’m doing that, I may as well bring some of that knowledge to the outside world as well, where appropriate.
All this to say: this article was adapted from something I wrote at work, in order to explain some subtleties about how smart pointers in modern C++1 work to people that only had experience with older C++. More specifically, this gets into how the way you use smart pointers should impact the design of libraries and their APIs.
-
For the purposes of this article, we’ll assume that “modern C++” means C++11 and later, whereas “older C++” means everything before C++11. ↩︎