We'll start by passing arguments to functions that can, but may not, hold value. Have you ever stumbled upon a function signature similar to the following?
void calculate(int param); // If param equals -1 it means "no value" void calculate(int param = -1);
Sometimes, it's just too easy to pass a -1 by mistake when you didn't want to if param was calculated somewhere else in code – perhaps where it was even a valid value. How about the following signature?
void calculate(std::optional<int> param);
This time, it's much clearer what to do if you don't want to pass a value: just pass an empty optional. The intent is clear, and -1 can still be used as a valid value instead of you having to give it any special meaning in a type-unsafe manner.
That's just one usage of our optional template. Let's see some others.