Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
std

The <charconv> header from C++17's standard library. More...

+ Collaboration diagram for std:

Functions

template<typename value_type >
requires std::is_floating_point_v<value_type>
to_chars_result sharg::contrib::charconv_float::to_chars_floating_point (char *first, char *last, value_type value) noexcept
 std::to_chars implementation for floating point via a std::stringstream for default base = 10.
 
template<typename value_type >
requires std::is_floating_point_v<value_type>
from_chars_result sharg::contrib::charconv_float::from_chars_floating_point (char const *first, char const *last, value_type &value, chars_format fmt=chars_format::general) noexcept
 Delegates to functions strto[d/f/ld] for floating point value extraction.
 
template<typename floating_point_type >
requires std::is_floating_point_v<floating_point_type>
to_chars_result sharg::contrib::charconv_float::to_chars (char *first, char *last, floating_point_type value) noexcept
 std::to_chars overload for floating point via a std::stringstream for default base = 10.
 
template<typename floating_point_type >
requires std::is_floating_point_v<floating_point_type>
from_chars_result sharg::contrib::charconv_float::from_chars (char const *first, char const *last, floating_point_type &value, chars_format fmt=chars_format::general) noexcept
 Parse a char sequence into an floating point value.
 

Detailed Description

The <charconv> header from C++17's standard library.

The following table describes what implementation of std::to_chars and std::from_chars will be used

stdlib version __cpp_lib_to_chars chars_format to_chars_result from_chars_result to_chars (int) from_chars (int) to_chars (float) from_chars (float)
gcc 10 undefined and <charconv> header stdlib stdlib stdlib stdlib stdlib shim (ostringstream) shim (strto[f/d/ld])
gcc 11 undefined (or 201611) and <charconv> header stdlib stdlib stdlib stdlib stdlib stdlib stdlib

Note: gcc 11 implements float too, but does not define __cpp_lib_to_chars

Function Documentation

◆ from_chars()

template<typename floating_point_type >
requires std::is_floating_point_v<floating_point_type>
from_chars_result sharg::contrib::charconv_float::from_chars ( char const * first,
char const * last,
floating_point_type & value,
chars_format fmt = chars_format::general )
inlinenoexcept

Parse a char sequence into an floating point value.

Template Parameters
floating_point_typeThe type to parse the string into; Must model std::is_floating_point_v.
Parameters
[in]firstThe start of the string to parse.
[in]lastThe end of the string to parse.
[in,out]valueThe value to store the parsed result in.
[in]fmtThe std::chars_format that alters the behaviour of parsing.
Returns
A std::from_char_result. See detail section return value for more information.

Analyzes the character sequence [first,last) for a pattern described below. If no characters match the pattern or if the value obtained by parsing the matched characters is not representable in the type of value, value is unmodified, otherwise the characters matching the pattern are interpreted as a text representation of an arithmetic value, which is stored in value.

Floating-point parsers: Expects the pattern identical to the one used by std::strtod in the default ("C") locale, except that:

  • the plus sign is not recognized outside of the exponent (only the minus sign is permitted at the beginning)
  • if fmt has std::chars_format::scientific set but not std::chars_format::fixed, the exponent part is required (otherwise it is optional)
  • if fmt has std::chars_format::fixed set but not std::chars_format::scientific, the optional exponent is not permitted
  • if fmt is std::chars_format::hex, the prefix "0x" or "0X" is not permitted (the string "0x123" parses as the value "0" with unparsed remainder "x123").
Attention
This implementation is a workaround until the function is supported by the compiler. It falls back to use the functions strto[d/f/ld] before checking the above limitations

Return value

This function is workaround until the function is supported by the compiler. It falls back to use the functions strto[d/f/ld] so the return value is NOT as documented here https://en.cppreference.com/w/cpp/utility/from_chars but:

On success, std::from_chars_result::ec is value-initialized. On error, std::from_chars_result::ec is either an std::errc::invalid_argument if an illegal character or format has been encountered, or std::errc::out_of_range if parsing the value would cause an overflow. The std::from_chars_result::ptr value is always set to last.

The locale issue

std::from_chars is documented to be locale independent. The accepted patterns are identical to the one used by strtod in the defailt ("C") locale.

The functions strto[d/f/ld] used here are locale dependent but setting the locale manually by std::setlocale is not thread safe. So for the time being this workaround is locale dependent.

See also
https://en.cppreference.com/w/cpp/utility/from_chars
Hide me