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.