C++17 introduced Algebraic Data Types that bring safer alternatives to error codes and unions.
std::optional
Represents a value that may or may not exist. Replaces nullptr or sentinel values (like -1).
std::optional<double> divide(double a, double b) {
if (b == 0) return std::nullopt;
return a / b;
}
std::variant
A type-safe union. A variant<int, string> can hold either an int or a string. Use std::get or std::visit (visitor pattern) to access.