...

/

The Standard Library Changes

The Standard Library Changes

This section delves deep into the std::optional utility introduced in C++17.

Introduction

While new language features allow you to write more compact code, you also need the tools - in the form of the Standard Library types. The classes and systems that you can find in the Library can significantly enhance your productivity. C++17 offers even more handy instruments: for example the filesystem, new vocabulary types, and even parallel algorithms! We’ll now look at a prominent new feature called std::optional.

Nullable Types

One approach is to achieve “null-ability” by using unique values (-1, infinity, nullptr). Before use, you need to compare the object against the predefined value to see if it’s not empty. Such a pattern is widespread in programming. For instance string::find returns a value that represents the position or npos when it’s “null” or the pattern is not found.

Alternatively, you ...