streambuf get streampos

c++, stl, stream, streambuf

Solution

The `streambuf` doesn't have a separate interface for reading the position. However, `istream` and `ostream` do (`tellg` and `tellp` respectively).

Interestingly, the streams use your option 2 to get their positions, so it is just fine.

Problem

I use the C++ `streambuf` class for a compiler project and need a convenient way to get the current position in the stream. There are two member functions, `streambuf::pubseekpos` and a `streambuf::pubseekoff`, to modify the position and I am quite confused about the absence of a `streambuf::pubgetpos` member function (or something similar) to read it. There seem to be two possible workarounds: I could save the current position in a separate variable and modify it manually whenever I read characters from the stream. I could call `streambuf::pubseekoff(0, ios_base::cur)`, which returns the new stream position. The second option seems usable but inefficient and unaesthetic for such a trivial task. Is there a better way to do it?

Original source