//******************************************************** // The following code example is taken from the book // C++17 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd17.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include #include #include // convert string to int if possible: std::optional asInt(const std::string& s) { try { return std::stoi(s); } catch (...) { return std::nullopt; } } int main() { for (auto s : {"42", " 077", "hello", "0x33"} ) { // try to convert s to int and print the result if possible: std::optional oi = asInt(s); if (oi) { std::cout << "convert '" << s << "' to int: " << *oi << "\n"; } else { std::cout << "can't convert '" << s << "' to int\n"; } } }