Why is tellg not const?

Why is tellg not const?

Post by alfp » Thu, 15 Sep 2005 05:23:17


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.yqcomputer.com/ ]
 
 
 

Why is tellg not const?

Post by hinnan » Thu, 15 Sep 2005 06:45:56

In article < XXXX@XXXXX.COM >,



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.yqcomputer.com/ ]