8

27.6.3.4.2 Buffer management and positioning

pos_type seekoff(off_type off, ios_base::seekdir way,
    ios_base::openmode which = ios_base::in | ios_base::out);
  • Effects: Alters the stream positions within one or more of the controlled sequences in a way that is defined separately for each class derived from basic_streambuf in this Clause (27.8.2.4, 27.9.1.5).
  • Default behavior: Returns pos_type(off_type(-1)).

So far, so good. The basic_streambuf derivation I'm using can alter its position separately for ios_base::in and/or ios_base::out. But, what do I need to return when both are specified?

If you specify ios_base::in or ios_base::out, we would return new stream position of the specific sequence.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
0xbadf00d
  • 14,584
  • 15
  • 60
  • 93
  • "_in a way that is defined separately for each class_" is that a specification, or a joke? – curiousguy Oct 09 '11 at 02:14
  • @curiousguy - Even if it sounds weird, it's indeed a specification. It allows derived classes to behave in a sensible way that may vary between different implementations and use cases. However, it's not always that intuitive how a implementation "should" behave. – 0xbadf00d Oct 09 '11 at 06:54
  • "_it's indeed a specification_" But it isn't. It's an allusion to some other specification somewhere else. "_It allows derived classes to behave in a sensible way that may vary between different implementations and use cases._" What's the point of this "specification"? What can be done knowing that `seekoff` alters the stream positions within one or more of the controlled sequences in a way that is defined separately for each derived class? – curiousguy Oct 09 '11 at 10:14
  • @curiousguy - From a top-down perspective where you're not aware of the actual implementation: You don't know how the stream positions are altered. But this concept allows varying implementations, behaving like a `streambuf`, but stay flexible in position handling. – 0xbadf00d Oct 09 '11 at 13:37
  • What can you safely do with `basic_streambuf::seekoff`? – curiousguy Oct 09 '11 at 15:31

2 Answers2

1

It is a bit up to your stream to define what happens. The built in streams differ, in that some can have separate read and write positions (stringstream) while others just have one (fstream).

If the user does a reposition and specify both in and out, perhaps you should move both. If it is a seek with a zero offset to get the current position, it is not unreasonable to fail if the positions differ.

Bo Persson
  • 86,087
  • 31
  • 138
  • 198
  • Another option would be to move both by the offset, but I don't know what I should return. I think you're right and it would be most reasonable to fail if the positions differ. – 0xbadf00d Jul 23 '11 at 15:01
0

Following seekoff from 27.8.2.4 it seems that you are expected to fail.

Check the condition table 130, which states that both input and output sequences should be positioned only if

(which & (ios_base::in | ios_base::out)) == (ios_base::in) | ios_base::out))
and way == either ios_base::beg or ios_base::end
Christopher Oezbek
  • 17,629
  • 3
  • 48
  • 71