CppCon 2020 has ended
Back To Schedule
Thursday, September 17 • 09:00 - 10:00
Back to Basics: Smart Pointers

Log in to save this to your schedule, view media, leave feedback and see who's attending!

Feedback form is now closed.
From the library's perspective, an essential feature in C++11 was smart pointers.

Since C++11, we have four different smart pointer: std::auto_ptr, std::unique_ptr, std::shared_pointer, and std::weak_ptr. With C++17 std::auto_ptr was removed and with C++20, we get an std::atomic<std::shared_ptr>, and std::atomic<std::weak_ptr>.

Each smart pointer models a specific ownership semantic and has a particular purpose. The crucial question is, therefore: When should you use which smart pointer? The answer to this question becomes more sophisticated when you think about the arguments or the return value of a function. You have to answer the following question for the function arguments:

- What does it mean to take the function arguments by pointer, by std::unique_ptr, or by std::shared_ptr?
- Should the function take the smart pointer by value or by reference?
- Should the argument be const or not?

The answers to those questions are not the end of the story. When you have a factory function creating something, the question immediately arises: Should the factory function return its product by a pointer, by std::unique_ptr, or by std::shared_ptr?

Smart pointers are more than pointers decorated with smartness. Smart pointer models ownership semantic. Understanding the ownership semantic of smart pointers is the main topic of my talk and, therefore, your takeaway.

avatar for Rainer Grimm

Rainer Grimm

C++ trainer, Modernes C++
I've worked as a software architect, team lead, and instructor since 1999. In 2002, I created company-intern meetings for further education. I have given training courses since 2002. My first tutorials were about proprietary management software, but I began teaching Python and C... Read More →

Thursday September 17, 2020 09:00 - 10:00 MDT
Back to Basics