Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!kpc.com!ardent!jss From: jss@gold.kpc.com (Jerry Schwarz) Newsgroups: comp.lang.c++ Subject: Re: istream::putback(char c) Message-ID: Date: 10 Apr 91 01:29:40 GMT References: <71302@brunix.UUCP> Sender: uucp@kpc.com (UNIX-to-UNIX Copy) Organization: Kubota Pacific Computer Lines: 38 In-Reply-To: sdm@cs.brown.edu's message of 8 Apr 91 19:00:07 GMT Nntp-Posting-Host: gold In article <71302@brunix.UUCP> sdm@cs.brown.edu (Scott Meyers) writes: From the istream man page: insp=&ins.putback(c) Attempts to back up ins.rdbuf(). c must be the character before ins.rdbuf()'s get pointer. (Unless other activity is modifying ins.rdbuf() this is the last character extracted from ins.) If it is not, the effect is undefined. This peculiar interface was motivated by a desire to make it easy to derive new streambufs. > 1. Why am I limited to putting back the last character I removed? > There is no such limitation for ungetc in the standard C library. It is possible that you are extracting characters from a readonly data structure. For example a strstreambuf that was initialized with a |const char*|. strstreambuf ignores the argument and just backs up the (internal) pointer. > 2. Given that I am limited to putting back the last character I > read, why does putback take a parameter? Putback was originally specified in a way that corresponded to ungetc. Some streambufs (e.g. filebuf) will use the character and do allow you to putback a character other than the one you extracted most recently. The restriction (that you should only putback the previously extracted character) was added later when I noticed the situation described in case 1. The X3J16 library working group is considering eliminating the argument. That is declaraing istream::putback() to back up the pointer. Jerry Schwarz