http://en.cppreference.com/w/cpp/utility/to_chars
Reference does not say anything about that, but the example is (for me) clearly using a null-terminated string, otherwise how could it know where to end, since std::array::data
returns only a pointer.
#include <iostream>
#include <charconv>
#include <array>
int main()
{
std::array<char, 10> str{};
std::to_chars(str.data(), str.data()+str.size(), 42);
std::cout << str.data();
}
Unfortunately I can't test it myself because AFAIK no compiler supports it yet: https://en.cppreference.com/w/cpp/compiler_support
Edit:
Forgot that str
is initialized with zeros, however question is still relevant.
The C++17 specification does not state that to_chars
adds a NUL terminator:
All functions named
to_chars
convertvalue
into a character string by successively filling the range [first
,last
) , where [first
,last
) is required to be a valid range. If the memberec
of the return value is such that the value, when converted tobool
, isfalse
, the conversion was successful and the memberptr
is the one-past-the-end pointer of the characters written. Otherwise, the memberec
has the valueerrc::value_too_large
, the memberptr
has the valuelast
, and the contents of the range [first
,last
) are unspecified.
Nothing is stated about a NUL terminator in that paragraph or in the paragraphs that specifically define the behavior of the individual to_chars
overloads. Therefore, it does not write one.
The example works, so long as to_chars
does not produce more than 9 characters. Since str
is initialized to all NUL characters, anything that is not written into str
will be left as NUL characters.
To add to this, the original paper P0067R0 that proposed it explicitly states that the to_chars
functions should not NUL-terminate the strings.