absl::from_chars

Workalike compatibility version of std::from_chars from C++17. Currently this only supports the double and float types.

Synopsis

Declared in <absl/strings/charconv.h>

absl::from_chars_result
from_chars(
    char const* first,
    char const* last,
    double& value,
    chars_format fmt = chars_format::general);

Description

This interface incorporates the proposed resolutions for library issues DR 3080 and DR 3081. If these are adopted with different wording, Abseil's behavior will change to match the standard. (The behavior most likely to change is for DR 3081, which says what value will be set to in the case of overflow and underflow. Code that wants to avoid possible breaking changes in this area should not depend on value when the returned from_chars_result indicates a range error.)

Searches the range [first, last) for the longest matching pattern beginning] at first that represents a floating point number. If one is found, store the result in value.

The matching pattern format is almost the same as that of strtod(), except that (1) C locale is not respected, (2) an initial '+' character in the input range will never be matched, and (3) leading whitespaces are not ignored.

If fmt is set, it must be one of the enumerator values of the chars_format. (This is despite the fact that chars_format is a bitmask type.) If set to scientific, a matching number must contain an exponent. If set to fixed, then an exponent will never match. (For example, the string "1e5" will be parsed as "1".) If set to hex, then a hexadecimal float is parsed in the format that strtod() accepts, except that a "0x" prefix is NOT matched. (In particular, in hex mode, the input "0xff" results in the largest matching pattern "0".)

Return Value

A from_chars_result describing where parsing stopped and any error.

Parameters

NameDescription
firstPointer to the first character of the input range.
lastPointer to one past the last character of the input range.
valueSet to the parsed number when a match is found.
fmtThe floating-point format to accept.