#include <iostream> #include <optional> #include <string> std::optional<int> parse_int(const std::string& s) { try { return std::stoi(s); } catch (...) { return std::nullopt; } } int main() { using namespace std; optional num = parse_int("bs"); // cout << *num << endl; // Prints uninitialzed value at address // cout << num.value() << endl; // Throws std::bad_optional_access // cout << num.value_or(0) << endl; // Prints default value 0 if (num) { cout << num.value() << endl; } // Prints nothing return 0; }