Topic: Why is tellg not const?


Author: alfps@start.no (Alf P. Steinbach)
Date: Tue, 13 Sep 2005 20:23:17 GMT
Raw View
Why is tellg not const?

(This is the third attempted posting of this question: it can't be rejected
for ever, can it?)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hinnant@metrowerks.com (Howard Hinnant)
Date: Tue, 13 Sep 2005 21:45:56 GMT
Raw View
In article <432665de.186071859@news.individual.net>,
 alfps@start.no (Alf P. Steinbach) wrote:

> Why is tellg not const?

Consider:

#include <streambuf>
#include <istream>
#include <cassert>

struct my_stream_buf
    : public std::streambuf
{
    typedef std::streambuf base;
protected:
    virtual base::pos_type seekoff(base::off_type off,
                                   std::ios_base::seekdir way,
                                   std::ios_base::openmode which =
                                        std::ios_base::in |
                                        std::ios_base::out)
    {throw 0; return base::pos_type(-1);}
};

int main()
{
    my_stream_buf sb;
    std::istream in(&sb);
    assert(in.good());
    in.tellg();
    assert(in.good());
}

The second assert fires.  tellg() has changed the observable state of
the istream in this example.

That being said, I can sympathize with your intent.  tellg does seem
like a non-modifying function and a seekoff that throws while seeking to
the current position is pathological.  If we were redesigning the I/O
classes today I would look for a way to avoid this contradiction
(perhaps a const interface in the streambuf to report current position
which could not throw).

-Howard

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]