[v3] fix libstdc++/9827

Pétur Runólfsson peturr02@ru.is
Wed Feb 26 09:09:00 GMT 2003


Jerry Quinn wrote:
> No, failed() is used to test if the sputn succeeded
> or not.
> The original working code within num_put::put
> (called by ostream::operator<<(int)) looked like:
> 
> *__s = c; ++__s
> 
> with __s being the ostream_iterator.  The code
> iterated over the string to be output.  In this
> case, it made N calls to operator=.  Operator= tries
> to write the char to the streambuf and sets
> _M_failed if it can't.  This is analogous, except it
> writes a full string at once, and again, sets
> _M_failed if it isn't able to write the string.

But ostreambuf_iterator::operator=(CharT) only writes to the
streambuf if failed() returns false, see 24.5.4.2
[lib.ostreambuf.iter.ops] p1.

Here is a test case:

#include <locale>
#include <ostream>
#include <iostream>
#include <iterator>

#undef NDEBUG
#include <cassert>

bool called = false;

class Buf : public std::streambuf
{
	virtual int_type overflow(int_type)
		{
			called = true;
			return traits_type::eof();
		}
};

int main()
{
	using namespace std;

	Buf b;
	ostream o(&b);
	
	ostreambuf_iterator<char> it (o);
	it = 'a';
	assert(it.failed());
	assert(called);

	called = false;
	use_facet<num_put<char> >(o.getloc()).put(it, o, ' ', 2L);
	assert(!called);

	return 0;
}

Petur



More information about the Libstdc++ mailing list