Consider the following example:
#include <format>
#include <print>
int main() {
int x = 12345;
char buffer[32] = "awoo";
std::format_to(buffer, "{}{}", x, buffer);
std::println("{}", buffer);
}
This code is expected to output 12345awoo
because we are formatting {}{}
with arguments 12345
and "awoo"
.
However, the actual output is 1234512345
. The reason for this is obvious: Formating 12345
would write to the buffer, and so by the time that buffer
(the second argument) is being handled by std::format_to
, it no longer contains "awoo"
.
This begs the question: What is the expected result according to the C++ standard? https://eel.is/c++draft/format.functions#lib:vformat_to does not specify as a precondition that aliasing between the arguments and the output buffer isn’t permitted, so I don’t believe that this case is undefined behavior.
5