Representing optional values with std::optional

Although quite a minor feature in C++17, std::optional is a neat addition to the STL library which simplifies a common case which couldn't be expressed in a clean straightforward syntax prior to std::optional. In a nutshell, it is a small wrapper for any type where the wrapped type can be both initialized and uninitialized.

To put it in C++ lingo, std::optional is a stack-allocated container with a max size of one.

Note that the Boost Libraries has had an equivalent of std::optional,named boost::optional for many years.

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.