Код: Выделить всё
int parse_int(std::string_view str) {
if (str.empty()) {
throw std::invalid_argument("string must not be empty");
}
/* ... */
if (/* result too large */) {
throw std::out_of_range("value exceeds maximum for int");
}
return result;
}
Код: Выделить всё
enum class parse_error {
empty_string,
invalid_format,
out_of_range
};
std::expected parse_int(std::string_view str) noexcept {
if (str.empty()) {
return std::unexpected(parse_error::empty_string);
}
/* ... */
if (/* result too large */) {
return std::unexpected(parse_error::out_of_range);
}
return result;
}
Подробнее здесь: https://stackoverflow.com/questions/764 ... exceptions