Skip to content

Glossary: C++ Beyond the Basics

std::optional

Also written: optional, std::nullopt, nullopt

A type holding either a value `T` or nothing, which you return as `std::nullopt`. Test it with `if (x)`, read it with `*x` or `->`, and use `.value_or` for a fallback.

Where the book teaches it

Related terms

std::expectedAttribute