I can find surprisingly little useful information about how an overridden std::streambuf::overflow function should look like to simply get each character that's written to the stream. So I asked ChatGPT for some pointers. It keeps coming back to this concept:
int overflow(int c)
{
if (c == EOF)
{
// Flush the buffer here
return !EOF;
}
// Put c in the buffer here
if (c == '\n')
{
// Flush the buffer here
}
return c;
}
It's super weird that it returns !EOF when c is EOF.
The docs don't elaborate what "success" means.
This page says it should return EOF (not !EOF) when called with EOF as the argument (or when signalling "a failure").
So: Is my suspicion correct that returning !EOF here is wrong and that I should return EOF instead?
Cookie points if you can tell me where ChatGPT got that idea from. I can't find return !EOF; anywhere else on the internet.
This is what the C++ standard has to say about the return value:
Thus, the specific return value doesn't much matter, just whether it's
traits::eof()or not. There's a (non-normative) convention you may want to follow.Re: what "success" means:
Thus, the function fails if it is unable to actually write to the underlying medium, or unable to restore the invariants. It succeeds if it managed both.