Nullable type in C++

std::optional is a type wrapper which wraps that type and an optional flag which says about the nullability of that type.

Sometimes we create an object which contains no valid data. We often a associate a flag or a special value, which decides the validity of the data. For example,

  • a pointer with value nullptr is a pointer that points to no location.
  • an id type with an allowed value of 0, which says the id is invalid if 0 or it’s a valid if more than 0.