How to flatten the nested std::optional?
19
2
note: this question was briefly marked as a duplicate of this, but it is not an exact duplicate since I am asking about std::optionals specifically. Still a good question to read if you care about general case. Assume I have nested optionals, something like this(dumb toy example): struct Person{ const std::string first_name; const std::optional<std::string> middle_name; const std::string last_name; }; struct Form{ std::optional<Person> person; }; and this spammy function: void PrintMiddleName(const std::optional<Form> form){ if (form.has_value() && form->person.has_value() && form->person->middle_name.has_value()) { std::cout << *(*(*form).person).middle_name << std::endl; }...