Passing smart pointers is a critical topic that is seldom addressed. This ends with the C++ core guidelines because they have six rules for passing std::shared_ptr and std::unique_ptr.
The six rules violate the import dry (don’t repeat yourself) principle for software development. Ultimately, we have only four rules that make our life as software developers much easier. Here are the rules.
- R.32: Take a
unique_ptr<widget>parameter to express that a function assumes ownership of a
- R.33: Take a
unique_ptr<widget>¶meter to express that a function reseats the
- R.34: Take a
shared_ptr<widget>parameter to express that a function is part owner
- R.35: Take a
shared_ptr<widget>¶meter to express that a function might reseat the shared pointer
- R.36: Take a
const shared_ptr<widget>¶meter to express that it might retain a reference count to the object ???
- R.37: Do not pass a pointer or reference obtained from an aliased smart pointer
Let’s start with the first two rules for std::unique_ptr.
If a function should take ownership of a Widget, you should take the std::unique_ptr<Widget> by copy. The consequence is that the caller has to move the std::unique_ptr<Widget> to make the code run.
Call (1) is fine, but call (2) breaks because you can not copy a std::unique_ptr. If your function only wants to use the Widget, it should take its parameter by the pointer or reference. The difference between a pointer and a reference is that a pointer can be a null pointer.
Sometimes a function wants to reseat a Widget. In this use case, you should pass the std::unique_ptr<Widget> by a non-const reference.
Now, the call (1) fails because you can not bind an rvalue to a non-const lvalue reference. This will not hold for the copy in (2). A lvalue can be bound to an lvalue reference. By the way. The call (0) will not only construct a new Widget(2003), but it will also destroy the old Widget(1998).
The next three rules to std::shared_ptr are repetitions; therefore, I will make one out of them.
R.34: Take a
shared_ptr<widget> parameter to express that a function is part owner, R.35: Take a
shared_ptr<widget>& parameter to express that a function might reseat the shared pointer, and R.36: Take a
const shared_ptr<widget>& parameter to express that it might retain a reference count to the object ???
Here are the three function signatures we have to deal with.
Let’s look at each function signature in isolation. What does this mean from the function perspective?
- void share(std::shared_ptr<Widget> shaWid): I’m for the lifetime of the function body a shared owner of the Widget. At the beginning of the function body, I will increase the reference counter; at the end, I will decrease the reference counter; therefore, the Widget will stay alive as long as I use it.
- void reseat(std::shared_ptr<Widget>& shaWid): I’m not a shared Widget owner because I will not change the reference counter. I have not guaranteed that the Widget will stay alive during the execution of my function, but I can reseat the resource. A non-const lvalue reference is more like I borrow the resource and can reseat it.
- void mayShare(const std::shared_ptr<Widget>& shaWid): I only borrow the resource. Neither can I extend the resource’s lifetime nor can I reseat the resource. You should use a pointer (Widget*) or a reference (Widget&) as a parameter instead because there is no added value in using a std::shared_ptr.
Let me present you with a short code snippet to clarify the rule.
globShared (1) is a globally shared pointer. The function shared takes its argument per reference (2). Therefore, the reference counter of shaPtr will not be increased, and the function share will not extend the lifetime of Widget(2011). The issue begins with (3). oldFunc accepts a pointer to the Widget; therefore, oldFunc has no guarantee that the Widget will stay alive during its execution. oldFunc only borrows the Widget.
The cure is quite simple. You have to ensure that the reference count of globShared will be increased before the call to the function oldFunc. This means you have to make a copy of the std::shared_ptr:
- Pass the std::shared_ptr by copying to the function shared:
- Make a copy of the shaPtr in the function shared:
The same reasoning also applies to std::unique_ptr, but I have no simple cure in mind because you can not copy a std::unique_ptr. I suggest you should clone your std::unique_ptr and, therefore, make a new std::unique_ptr.
This was the last of my four posts about resource management in the C++ core guidelines. The C++ core guidelines have more than 50 rules for expressions and statements. I will have a closer look at my next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, and Bhavith C Achar.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,