Topic: basic_istream::ignore() and numeric_limits<int>::max()


Author: lijewski@mothra.lbl.gov (Mike Lijewski)
Date: 1996/06/25
Raw View
The following statement in section 27.6.1.3 in the 26 September 1995 Draft
is in regards to the the effect of basic_istream::ignore():

    if n != numeric_limits<int>::max() (18.2.1), n characters are extracted

What happens when n == numeric_limits<int>::max() ?

Why is numeric_limits<int>::max() special in basic_istream::ignore() ?

I have code that uses the equivalent INT_MAX constant from <limits.h> to
ignore arbitrary numbers of characters.   Is this now broken?
--
Mike Lijewski                                       mjlijewski@lbl.gov
Center for Computational Sciences and Engineering   phone: 510-486-8667
Lawrence Berkeley National Laboratory               fax:   510-486-6900
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Nathan Myers <ncm@cantrip.org>
Date: 1996/06/26
Raw View
Mike Lijewski wrote:
>
> The following statement in section 27.6.1.3 in the 26 September 1995 Draft
> is in regards to the the effect of basic_istream::ignore():
>
>     if n != numeric_limits<int>::max() (18.2.1), n characters are extracted
>
> What happens when n == numeric_limits<int>::max() ?
>
> Why is numeric_limits<int>::max() special in basic_istream::ignore() ?
>
> I have code that uses the equivalent INT_MAX constant from <limits.h> to
> ignore arbitrary numbers of characters.   Is this now broken?

You have to read it pretty carefully, but it really does say that
if n == INT_MAX, then it will read an arbitrary number of characters.

Nathan Myers
ncm@cantrip.org


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/06/28
Raw View
lijewski@mothra.lbl.gov (Mike Lijewski) writes:

>The following statement in section 27.6.1.3 in the 26 September 1995 Draft
>is in regards to the the effect of basic_istream::ignore():
>
>    if n != numeric_limits<int>::max() (18.2.1), n characters are extracted
>
>What happens when n == numeric_limits<int>::max() ?

You missed the context.  Here's the relevant text from 27.6.1.3:

|   basic_istream<charT,traits>&
|       ignore(int n = 1, int_type delim = traits::eof());
|
|   Effects:
|     Extracts characters and discards  them.   Characters  are  extracted
|     until any of the following occurs:
|
|   --if n  != numeric_limits<int>::max() (_lib.limits_), n characters are
|     extracted
|
|   --end-of-file occurs on the input sequence (in which case the function
|     calls    setstate(eofbit),   which   may   throw   ios_base::failure
|     (_lib.iostate.flags_));
|
|   --c == delim for the next available input character c (in which case c
|     is extracted).

So if n is equal to numeric_limits<int>::max(), then it will extract
and discard characters until end-of-file occurs or until it reaches
the delimiter.

>Why is numeric_limits<int>::max() special in basic_istream::ignore() ?
>
>I have code that uses the equivalent INT_MAX constant from <limits.h> to
>ignore arbitrary numbers of characters.   Is this now broken?

The reason that numeric_limits<int>::max() is special in
basic_istream::ignore() is, I believe, precisely to support code like
yours.  If it wasn't special, your code would be broken, because
INT_MAX is a finite number (it can be as low as 32767), and your code
would only ignore the first INT_MAX characters, rather than ignoring an
arbitrary number of them.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]