|
BDE 4.14.0 Production release
|
Provide implementations for functions not in the system library.
Canonical header: bsl_charconv.h
This component is for internal use only. Please include <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.
In this section we show intended use of this component.
Suppose we want to write a function that writes an int to a streambuf. We can use bsl::to_chars to write the int to a buffer, then write the buffer to the streambuf.
First, we declare our function:
Then, we declare a buffer long enough to store any int value in decimal.
Next, we declare a variable to store the return value:
Then, we call the function:
Next, we check that the buffer was long enough, which should always be the case:
Now, we check that sts.ptr is in the range [ buffer + 1, buffer + sizeof(buffer) ], which will always be the case whether to_chars succeeded or failed.
Finally, we write our buffer to the streambuf: