This chapter covers a pattern that can be seen as a generalization of the RAII idiom we studied earlier. In its earliest form, it is an old and established C++ pattern, however, it is also one that has particularly benefited from the language additions in C++11, C++14, and C++17. We will witness the evolution of this pattern as the language becomes more powerful. The ScopeGuard pattern exists at the intersection of the declarative programming (say what you want to happen, not how you want it done) and error-safe programs (especially exception safety). We will have to learn a bit about both before we fully understand the ScopeGuard.
The following topics will be covered in this chapter:
- How can we write error-safe and exception-safe code?
- How does RAII make error handling easier?
- What is composability as applied to error handling?
- Why is RAII not powerful enough for...