With std::optional
The lesson shows why std::optional is the right choice for nullable types.
We'll cover the following
How to Refactor using std::optional?
From the std::optional
chapter:
std::optional
is a wrapper type to express “null-able” types. It either contains a value, or it’s empty. It doesn’t use any extra memory allocation.
That seems to be the perfect choice for our code. We can remove the ok
variable and rely on the
semantics of the optional.
The new version of the code:
Get hands-on with 1400+ tech skills courses.