This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

libstdc++/12048


Hi,

This bug is about cin.unget() no longer working. 

Any fix for this must meet the conditions in DR 49:

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#49

In particular:

for any sequence of characters; the effect of extracting a character c by
  c = fgetc(f);
is the same as the effect of:
  c = str.rdbuf()->sbumpc(c);

This seems to mean that if characters read by cin.rdbuf()->sbumpc()
can be ungotten, then it must be possible to unget characters with
cin.unget() that were read with fgetc(stdin), that is [1]:

char c1;
char c2;
c1 = static_cast<unsigned char>(std::fgetc(stdin));
std::cin.unget();
std::cin.get(c2);
VERIFY (c1 == c2);

I see only three possible ways to fix this:

1) Document that cin.unget() no longer does anything.
2) Retrieve the last character read from stdin in
some platform-dependent manner and call std::ungetc(c, stdin);
3) Store the last character read with cin.rdbuf()->sbumpc() or
cin.rdbuf()->sgetn() and call std::ungetc(c, stdin);

I don't like 1) since cin.ungetc() has worked for a very long time and
lots of code may depend on being able to unget at least one character
(this is true in C and was true of classic IOstreams).

I'm not sure that 2) is possible; in case of unbuffered input the last
character might not be available at all, and in any case it requires
an implementation for each platform.

Solution 3) doesn't quite meet the requirements of DR 49, but only in
a rather obscure corner case which I don't expect to be present in
any real world code [1]. This is very simple to implement.

Any thoughts?
Petur

[1] The current text of the proposed resolution seems to contain a
number of errors; it's quite possible that this requirement was not
intended and the resolution can be modified to make it go away.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]