Converting strings to numbers

The reverse problem of formatting numbers for output is parsing numbers from the user's input. Parsing is intrinsically much more subtle and difficult than formatting, because we must account for the possibility of error. Every number can plausibly be turned into a string of digits, but not every string (or even every string of digits!) can plausibly be turned into a number. So, any function that purports to parse numbers must have some way of dealing with strings that do not represent valid numbers.

Consider the following declarations:

    std::istringstream iss;    std::string str = "42";    char buffer[] = "42";    int intvalue;    float floatvalue;    int rc;    char *endptr;    size_t endidx;    std::from_chars_result r;

To convert ...

Get Mastering the C++17 STL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.