Make illegal states unrepresentable: unions over flags, parse don't validate, smart constructors
#Name alternatives and tagged variants (Types & syntax) shows how to write a union; this entry uses unions and validated constructors to design a model in which nonsense combinations cannot be built. A class with a private constructor and a static parse returning std::expected<Email, std::string> (C++23) guarantees that every Email has been validated. std::variant replaces groups of optional fields and flags: Reach is one of ByEmail, ByPhone or ByBoth, so a contact with no way to reach it cannot exist. parse_contact runs once at the boundary and returns either a complete Contact or an error string, and std::visit makes each consumer handle every shape.
Complete program
Complete program.
In practice: keep invariants in classes whose constructors cannot produce an invalid object (private constructor plus a factory, or a constructor that throws), and avoid std::optional fields whose presence depends on another field. Before C++23, return std::optional or throw from the factory instead of std::expected.
Copying and moving still work with a private constructor (the implicit copy and move constructors stay public), which is what lets Contact hold an Email by value.
A moved-from Email has an unspecified string; if that matters, delete the move operations or re-check in accessors.