Skip to main content

Practice ยท 1 of 1

Parse a positive integer into a variant

Implement std::variant<int, std::string> parse_positive(const std::string& s). On success (a syntactically valid integer literal greater than 0, fitting int) return the value; otherwise return the error string "invalid: <s>". An optional leading + is allowed; -5, abc, an empty string, and 0 are invalid. Overflow beyond INT_MAX is invalid.

Difficulty: intermediate

Back to lesson: Practice: variant practice