Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use std::from_chars to parse an integer or floating-point value from a specific character range without requiring a null terminator. Include <charconv>, pass the range’s beginning and one-past-the-end pointers, then check both ec and ptr: the error code reports conversion status, and the pointer shows how much input was consumed.

Call std::from_chars with a bounded range

The function accepts a half-open range, [first, last), and writes the parsed value to an output variable. The range may refer to a std::string, a std::string_view, or another contiguous character buffer; it does not need a terminating null character.

#include <charconv>
#include <string_view>
#include <system_error>

std::string_view input = "1234";
int value{};
auto result = std::from_chars(input.data(), input.data() + input.size(), value);

if (result.ec == std::errc{} && result.ptr == input.data() + input.size()) {
    // The entire input was a valid integer.
}

The result is a std::from_chars_result with two members: ptr and ec. A successful conversion does not necessarily mean that the entire range was a number. If your input must contain only the number, require result.ptr == last as well as a clear error code. This API is specified as locale-independent, non-allocating, and non-throwing. cppreference’s from_chars reference and the C++ working draft document its interface and behavior.

Check errors and trailing characters

Handle the two failure cases explicitly. A no-match error means the input did not begin with a number in the accepted syntax. An out-of-range error means a number was recognized but could not fit in the destination type.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Result ec ptr Output value
Successful conversion Value-initialized; compare with std::errc{} First character not consumed, or last if the range was fully consumed Contains the converted number
No characters match std::errc::invalid_argument Equals first Unchanged
Matched number is outside the destination type’s range std::errc::result_out_of_range End of the matched portion Unchanged

For example, parsing "42xyz" as an integer can convert 42 successfully and leave ptr at the x. Treat that as a valid prefix only if your format permits trailing data. For validation of a whole field, compare the result pointer with the range end even when ec indicates success. The error and pointer behavior is specified in the C++ working draft.

Integer parsing rules

For integer overloads, the base defaults to 10 and must be between 2 and 36, inclusive. The accepted pattern is related to the C locale’s strtol pattern, but several details commonly trip up code migrated from C parsing routines:

  • Leading whitespace is not skipped. Trim it or reject it before calling if your input format allows or requires whitespace.
  • Only - is accepted as a sign, and only when the destination type is signed. A leading + is not accepted.
  • Base 16 does not consume a 0x or 0X prefix. If the input includes that prefix, handle it separately or parse the digits after it.

These are part of the documented integer pattern; see cppreference.

Floating-point parsing rules

Floating-point overloads default to std::chars_format::general. They also use a deliberately narrow grammar that differs from what programmers may expect from functions such as strtod:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Leading whitespace is not skipped.
  • A leading + is not accepted, though a plus sign may appear in an exponent, as in 1e+3.
  • With std::chars_format::scientific alone, an exponent is required.
  • With std::chars_format::fixed alone, an exponent is not permitted.
  • With std::chars_format::hex, omit the 0x prefix.

Choose the format that matches the input specification, then apply the same ec and ptr checks used for integers. For the exact pattern details, consult cppreference’s reference and the working draft.

Why use it, and when does it fit?

std::from_chars is designed for parsing machine-readable numeric text in a known range. Its locale-independent behavior makes the accepted syntax predictable regardless of the program’s locale, while its non-allocating and non-throwing interface can suit code that needs explicit error handling. Microsoft Learn describes the conversion functions as “tuned for performance” and says they support shortest-round-trip behavior; that is vendor documentation, not a guarantee of a specific speedup on every compiler or workload. Microsoft Learn’s <charconv> documentation provides its implementation guidance.

For output, std::to_chars is the natural companion. The exact recovery guarantee for every floating-point value emitted by to_chars applies when the corresponding from_chars and to_chars calls come from the same implementation. Do not assume an identical cross-library round trip for every floating-point value. cppreference’s from_chars documentation describes this relationship.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Standard and library support

The original std::from_chars facility is part of C++17. Newer capabilities depend on the standard library as well as the language mode selected for compilation; setting a newer language standard does not by itself prove that a library implements a facility.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
  • Constexpr integral conversions: the reference lists __cpp_lib_constexpr_charconv == 202207L for the C++23 addition.
  • Charconv testing support: the reference lists __cpp_lib_to_chars == 202306L for C++26 testing of <charconv> success or failure.

Check the feature-test macros and the documentation for the target standard library before relying on those newer capabilities. The feature table is documented in cppreference’s reference. A proposal for span-based overloads, P2584R0, is a proposal and does not establish that such overloads are available in a particular standard or library release.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.