问题
I am trying to implement a stream buffer and I'm having trouble with making overflow()
work. I resize the buffer by 10 more characters and reset the buffer using setp
. Then I increment the pointer back where we left off. For some reason the output is not right:
template <class charT, class traits = std::char_traits<charT>>
class stringbuf : public std::basic_stringbuf<charT, traits>
{
public:
using char_type = charT;
using traits_type = traits;
using int_type = typename traits::int_type;
public:
stringbuf()
: buffer(10, 0)
{
this->setp(&buffer.front(), &buffer.back());
}
int_type overflow(int_type c = traits::eof())
{
if (traits::eq_int_type(c, traits::eof()))
return traits::not_eof(c);
std::ptrdiff_t diff = this->pptr() - this->pbase();
buffer.resize(buffer.size() + 10);
this->setp(&buffer.front(), &buffer.back());
this->pbump(diff);
return traits::not_eof(traits::to_int_type(*this->pptr()));
}
// ...
std::basic_string<charT> str()
{
return buffer;
}
private:
std::basic_string<charT> buffer;
};
int main()
{
stringbuf<char> buf;
std::ostream os(&buf);
os << "hello world how are you?";
std::cout << buf.str();
}
When I print the string it comes out as:
hello worl how are ou?
It's missing the d
and the y
. What did I do wrong?
回答1:
The first thing to not is that you are deriving from std::basic_stringbuf<char>
for whatever reason without overriding all of the relevant virtual functions. For example, you don't override xsputn()
or sync()
: whatever these functions end up doing you'll inherit. I'd strongly recommend to derive your stream buffer from std::basic_streambuf<char>
instead!
The overflow()
method announces a buffer which is one character smaller than the string to the stream buffer: &buffer.back()
isn't a pointer to the end of the array but to the last character in the string. Personally, I would use
this->setp(&this->buffer.front(), &this->buffer.front() + this->buffer.size());
There is no problem so far. However, after making space for more characters you omitted adding the overflowing character, i.e., the argument passed to overflow()
to the buffer:
this->pbump(diff);
*this->pptr() = traits::to_char_type(c);
this->pbump(1);
There are a few more little things which are not quite right:
- It is generally a bad idea to give overriding
virtual
functions a default parameter. The base class function already provides the default and the new default is only picked up when the function is ever called explicitly. The string returned may contain a number of null characters at the end because the held string is actually bigger than sequence which was written so far unless the buffer is exactly full. You should probably implement the
str()
function differently:std::basic_string<charT> str() const { return this->buffer.substr(0, this->pptr() - this->pbase()); }
- Growing the string by a constant value is a major performance problem: the cost of writing
n
characters isn * n
. For largern
(they don't really need to become huge) this will cause problems. You are much better off growing yourbuffer
exponentially, e.g., doubling it every time or growing by a factor of1.5
if you feel doubling isn't a good idea.
来源:https://stackoverflow.com/questions/19849143/what-is-wrong-with-my-implementation-of-overflow