 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alf P. Steinbach Guest
|
Posted: Tue Sep 13, 2005 8:23 pm Post subject: Why is tellg not const? |
|
|
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 ]
|
|
| Back to top |
|
 |
Howard Hinnant Guest
|
Posted: Tue Sep 13, 2005 9:45 pm Post subject: Re: Why is tellg not const? |
|
|
In article <432665de.186071859 (AT) news (DOT) individual.net>,
[email]alfps (AT) start (DOT) no[/email] (Alf P. Steinbach) wrote:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|