Safely convert std::string_view to int (like stoi or atoi)
问题 Is there a safe standard way to convert std::string_view to int ? Since C++11 std::string lets us use stoi to convert to int : std::string str = "12345"; int i1 = stoi(str); // Works, have i1 = 12345 int i2 = stoi(str.substr(1,2)); // Works, have i2 = 23 try { int i3 = stoi(std::string("abc")); } catch(const std::exception& e) { std::cout << e.what() << std::endl; // Correctly throws 'invalid stoi argument' } But stoi does not support std::string_view . So alternatively, we could use atoi ,