-
Book Overview & Buying
-
Table Of Contents
Software Architecture with C++ - Second Edition
By :
Going back from ABIs to APIs, let’s mention one more type that we omitted when we were designing great APIs earlier in this book. The hero of this section can save the day when it comes to optional parameters for functions, as it can help your types have components that may or may not hold value, and it can also be used for designing clean interfaces or as a replacement for pointers. This hero is called std::optional and was standardized in C++17. If you can’t use C++17, you can still find it in Abseil (absl::optional) or find a very similar version from Boost (boost::optional). A big plus of using those classes is that they express the intent very clearly, which helps with writing clean and self-documenting interfaces. Let’s look at some use cases to see it in action.
We’ll start by passing arguments to functions that can, but may not, hold value. Have you ever stumbled upon a function signature...