473,388 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

why is ostream::tellp not const?

Hi,

I have an application where I want to check if the buffer of a given
ostream contains any characters or it is empty. The same ostream may be
checked from multiple threads. Thread safety is guaranteed by having
only const-access in every thread to the ostream. I believe that STL
streams, just like STL containers are thread-safe if you only do const
operations on them. But it turned out that the tellp method of ostream
is defined as non-const by the standard. The standard defines tellp as:

if fail() != false, returns pos_type(1) to indicate failure.
Otherwise, returns rdbuf()->*pubseekoff(0, cur, out).

Clearly neither of these need write access to the ostream itself. This
can be proven by calling the fail(), rdbuf() and pubseekoff() methods
from client code through a const ostream reference. The trick in this is
that rdbuf() is a const method of ostream returning a non-const pointer
to the buffer. Through this non-const buffer pointer the client code can
call the pubseekoff method directly.

int client_code_tellp(const ostream &o)
{
if(o.fail()) return pos_type(1);
else return o.rdbuf()->pubseekoff(0, cur, out);
}

Now the possibility of this from client code suggests that the C++
standard is buggy in not declaring the tellp method itself const.

Another idea I am having: is this client_code_tellp threadsafe? Can it
be called from multiple threads on the same ostream? If the pubseekoff
is a non-const method then it can write to any member of the buffer,
which would be catastrophic for multithreading. Logically it should not,
if I am seeking 0 elements, but it has the possibility. Is there any
guarantee for this? It would be much cleaner if the buffer would provide
a separate const tellp method itself, instead of having to use a
non-const method just to get out some information.

Can somebody provide some insight?

- Sandor
Jul 23 '05 #1
0 3608

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
2
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
9
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
1
by: Edward Diener | last post by:
If you run this simple console program after building it on VC++ .NET 2003, you will see the error message, "Error getting initial stream position", from the attempt to use the...
5
by: truce | last post by:
I searched but couldn't find anything _quite_ like this. I have some code I inherited that needs some work. Basically I have a template that is instantiated in the c++ code and so only the...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.