Quick Links: |
Provide implementations for functions not in the system library. More...
Namespaces | |
namespace | bslstl |
<bsl_charconv.h>
instead. This component provides implementations for standard algorithms that are not provided by the underlying standard library implementation. For example, to_chars
is a C++17 algorithm, and it is provided here for code using C++03 - C++14 or for compilers that do not provide <charconv>
in C++17. to_chars
is locale-independent, non-allocating, and non-throwing, and provides a safe and more performant alternative to snprintf
in contexts where complex formatting options or locale are not important. int
to a streambuf
. We can use bsl::to_chars
to write the int
to a buffer, then write the buffer to the streambuf
. void writeJsonScalar(std::streambuf *result, int value) // Write the specified 'value', in decimal, to the specified 'result'. {
int
value in decimal. char buffer[11]; // size large enough to write 'INT_MIN', the // worst-case value, in decimal.
bsl::to_chars_result sts;
sts = bsl::to_chars(buffer, buffer + sizeof(buffer), value);
assert(bsl::ErrcEnum() == sts.ec);
sts.ptr
is in the range [ buffer + 1, buffer + sizeof(buffer) ]
, which will always be the case whether to_chars
succeeded or failed. assert(buffer < sts.ptr);
assert(sts.ptr <= buffer + sizeof(buffer));
streambuf
: result->sputn(buffer, sts.ptr - buffer); }