Operations on std::optional

Creating and accessing is just the tip of the iceberg. There are several operations that can be performed on std::optional.

Let’s see what other operations are available for the type.

Changing the Value & Object Lifetime

If you have an existing optional object, then you can quickly change the contained value by using several operations like emplace, reset, swap, assign. If you assign (or reset) with a nullopt then if the optional contains a value, its destructor will be called.

Here’s a quick summary:

Get hands-on with 1200+ tech skills courses.