std::to_chars(3)
β¨ std::to_chars β C++ Standard Libary π
πΈ NAME πΈ
std::to_chars - std::to_chars
π Synopsis π
Defined in header <charconv>
std::to_chars_result
(since
C++17)
to_chars( char* first, char* last, (1)
(constexpr since C++23)
/* integer-type */ value, int base = 10 );
std::to_chars_result (2) (since
C++17)
to_chars( char*, char*, bool, int = 10 ) = delete;
std::to_chars_result
to_chars( char* first, char* last, /* (3) (since
C++17)
floating-point-type */ value );
std::to_chars_result
to_chars( char* first, char* last, /* (4) (since
C++17)
floating-point-type */ value,
std::chars_format fmt );
std::to_chars_result
to_chars( char* first, char* last, /* (5) (since
C++17)
floating-point-type */ value,
std::chars_format fmt, int precision );
π DESCRIPTION π
Converts value into a character string by successively filling the range [first, last), where [first, last) is required to be a valid range.
- Integer formatters: value is converted to a string of digits in the given base (with no redundant leading zeroes). Digits in the range
10..35(inclusive) are represented as lowercase charactersa..z. If value is less than zero, the representation starts with a minus sign. The library provides overloads for all cv-unqualified (since C++23) signed and unsigned integer types and for the typecharas the type of the parameter value. - Overload for
boolis deleted.std::to_charsrejects argument of typeboolbecause the result would be"0"/"1"but not"false"/"true"if it is permitted. - value is converted to a string as if by
std::printfin the default ("C") locale. The conversion specifier isfore(resolving in favor offin case of a tie), chosen according to the requirement for a shortest representation: the string representation consists of the smallest number of characters such that there is at least one digit before the radix point (if present) and parsing the representation using the correspondingstd::from_charsfunction recovers value exactly. If there are several such representations, one with the smallest difference to value is chosen, resolving any remaining ties using rounding according tostd::round_to_nearest. The library provides overloads for all cv-unqualified standard (until C++23) floating-point types as the type of the parameter value. - Same as (3), but the conversion specified for the as-if
printfisfif fmt isstd::chars_format::fixed,eif fmt isstd::chars_format::scientific,a(but without leading"0x"in the result) if fmt isstd::chars_format::hex, andgif fmt ischars_format::general. The library provides overloads for all cv-unqualified standard (until C++23) floating-point types as the type of the parameter value. - Same as (4), except the precision is specified by the parameter precision rather than by the shortest representation requirement. The library provides overloads for all cv-unqualified standard (until C++23) floating-point types as the type of the parameter value.
π Parameters π
first,lastβ character range to write tovalueβ the value to convert to its string representationbaseβ integer base to use: a value between2and36(inclusive).fmtβ floating-point formatting to use, a bitmask of typestd::chars_formatprecisionβ floating-point precision to use
π Return value π
On success, returns a value of type std::to_chars_result such that ec equals value-initialized std::errc and ptr is the one-past-the-end pointer of the characters written. Note that the string is not NUL-terminated.
On error, returns a value of type std::to_chars_result holding std::errc::value_too_large in ec, a copy of the value last in ptr, and leaves the contents of the range [first, last) in unspecified state.
π Exceptions π
Throws nothing.
π§ Notes π§
Unlike other formatting functions in C++ and C libraries, std::to_chars is locale-independent, non-allocating, and non-throwing. Only a small subset of formatting policies used by other libraries (such as std::sprintf) is provided. This is intended to allow the fastest possible implementation that is useful in common high-throughput contexts such as text-based interchange (JSON or XML).
The guarantee that std::from_chars can recover every floating-point value formatted by std::to_chars exactly is only provided if both functions are from the same implementation.
It is required to explicitly cast a bool value to another integer type if it is wanted to format the value as "0"/"1".
πΈ Feature-test macro πΈ
__cpp_lib_to_charsβ201611L(C++17) β Elementary string conversions (std::to_chars,std::from_chars)__cpp_lib_to_charsβ202306L(C++26) β Testing for success or failure of<charconv>functions__cpp_lib_constexpr_charconvβ202207L(C++23) β Add constexpr modifiers tostd::to_charsandstd::from_charsoverloads (1) for integral types
π Example π
// Run this code
#include <array>
#include <charconv>
#include <iostream>
#include <string_view>
#include <system_error>
void show_to_chars(auto... format_args)
{
std::array<char, 10> str;
#if __cpp_lib_to_chars >= 202306L
// use C++26 operator bool() for error checking
if (auto res = std::to_chars(str.data(), str.data() + str.size(),
format_args...))
std::cout << std::string_view(str.data(), res.ptr) << '\n';
else
std::cout << std::make_error_code(res.ec).message() << '\n';
#else
if (auto [ptr, ec]
= std::to_chars(str.data(), str.data() + str.size(),
format_args...);
ec == std::errc())
std::cout << std::string_view(str.data(), ptr) << '\n';
else
std::cout << std::make_error_code(ec).message() << '\n';
#endif
}
int main()
{
show_to_chars(42);
show_to_chars(+3.14159F);
show_to_chars(-3.14159, std::chars_format::fixed);
show_to_chars(-3.14159, std::chars_format::scientific, 3);
show_to_chars(3.1415926535, std::chars_format::fixed, 10);
}
Possible output:
42
3.14159
-3.14159
-3.142e+00
Value too large for defined data type
π οΈ Defect reports π οΈ
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 2955(C++17) β behavior as published: this function was in<utility>and usedstd::error_code; correct behavior: moved to<charconv>and usesstd::errcLWG 3266(C++17) β behavior as published:boolargument was accepted and promoted toint; correct behavior: rejected by a deleted overloadLWG 3373(C++17) β behavior as published:std::to_chars_resultmight have additional members; correct behavior: additional members are disallowed
π See also π
to_chars_resultβ the return type ofstd::to_chars(C++17) (class)from_charsβ converts a character sequence to an integer or floating-point value (C++17) (function)to_stringβ converts an integral or floating-point value to string (C++11) (function)printf,fprintf,sprintf,snprintfβ prints formatted output to stdout, a file stream or a buffer (function);snprintfis tagged(C++11)operator<<β inserts formatted data (public member function ofstd::basic_ostream<CharT,Traits>)
π that's the whole page, bestie! source: http://cppreference.com, dated 2024.06.10, page std::to_chars(3) β now go format something iconic π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms