libstdc++/9661: cin.rdbuf()->sputbackc() does not put characters back to stdin
Tue Feb 11 12:56:00 GMT 2003

>Number:         9661
>Category:       libstdc++
>Synopsis:       cin.rdbuf()->sputbackc() does not put characters back to stdin
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Feb 11 12:56:00 UTC 2003
>Release:        gcc-3.2.1
Red Hat Linux 8.0
According to this posting to comp.lang.c++.moderated by Matt Austern:

cin.rdbuf()->sputbackc(c) and ungetc(c, stdin) should be equivalent and interchangable.

Although this is a QoI issue, it seems logical to fix this since
* cin.rdbuf()->sbumpc() and fgetc(stdin) are equivalent;
* characters returned by cin.rdbuf()->sgetc() can be consumed by both cin.rdbuf()->sbumpc() and fgetc(stdin)
* characters put back by ungetc will be returned by cin;
that is, cin.rdbuf()->sputbackc is the only case where the effect on cin and stdin differs.
See attachment.

Content-Type: text/plain; name=""
Content-Disposition: inline; filename=""

#include <cstdio>
#include <iostream>

#undef NDEBUG
#include <cassert>

int main()
	using namespace std;

	int c1 = fgetc(stdin);
	int c2 = cin.rdbuf()->sputbackc(c1);
	assert(c2 == c1);

	int c3 = fgetc(stdin);
	assert(c3 == c1);
	ungetc(c3, stdin);

	int c4 = cin.rdbuf()->sgetc();
	assert(c4 == c3);

	return 0;

More information about the Gcc-bugs mailing list