//******************************************************** // 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 // for from_chars() #include // convert string to int if possible: std::optional asInt(std::string_view sv) { int val; // read character sequence into the int: auto [ptr, ec] = std::from_chars(sv.data(), sv.data()+sv.size(), val); // if we have an error code, return no value: if (ec != std::errc{}) { return std::nullopt; } return val; } 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"; } } }