Achieving coherence in a class state is not always an easy task. For instance, sometimes, you want to have a member or two that can simply not be set. Instead of creating another class for such a case (which increases code complexity) or reserving a special value (which is easy to pass unnoticed), you can use an optional class member. Consider the following type:
struct UserProfile {
std::string nickname;
std::optional <std::string> full_name;
std::optional <std::string> address;
std::optional <PhoneNumber> phone;
};
Here, we can see which fields are necessary and which ones don't need to be filled. The same data could be stored using empty strings, but this wouldn't be clearly visible just from the struct's definition. Another alternative would be to use std::unique_ptr's, but then we would lose data locality, which is often essential for performance. For such cases, std::optional can be of great value. It should definitely...