Smart Pointers: Passing Smart Pointers
Explore how to pass smart pointers in Modern C++ effectively by following core guideline rules. Understand when to transfer ownership, borrow, or reseat std::unique_ptr and std::shared_ptr, enabling better resource management in safety-critical embedded systems.
We'll cover the following...
We'll cover the following...
Passing smart pointers is an important topic that is seldom addressed. This process ends with the C++ core guidelines since they have six rules for passing std::shared_ptr and std::unique_ptr.
The Six Rules
The following six rules violate the import dry (don’t repeat yourself) principle for software development. At the end, we have only four rules, which makes life as a software developer a lot easier. Here are the rules.
- R.32: Take a
unique_ptr<widget>parameter to express that a function assumes ownership of a widget - R.33: Take a
unique_ptr<widget>¶meter to express that a function reseats the widget