22.5 — std::unique_ptr

At the beginning of the chapter, we discussed how the use of pointers can lead to bugs and memory leaks in some situations. For example, this can happen when a function early returns, or throws an exception, and the pointer is not properly deleted. #include <iostream> void someFunction() { auto* …

22.4 — std::move

Once you start using move semantics more regularly, you’ll start to find cases where you want to invoke move semantics, but the objects you have to work with are l-values, not r-values. Consider the following swap function as an example: #include <iostream> #include <string> template <typename T> void mySwapCopy(T& a, …

22.2 — R-value references

In chapter 12, we introduced the concept of value categories (), which is a property of expressions that helps determine whether an expression resolves to a value, function, or object. We also introduced l-values and r-values so that we could discuss l-value references. If you’re hazy on l-values and r-values, …