[general/iostreams/iterators] Why not a forward and/or a random access istream iterator?
Hello, does anybody know whether there is something fundamentally wrong with designing an istream iterator that conforms with the forward, or even random access, iterator concepts? The std::istream_iterator conforms only with the simplest input iterator concept (stream need not support positioning). If a stream supports positioning, I cannot see why this should not work. If a streamable T has variable size on stream, we could do forward iteration, and if T has a known constant size on the stream, we could do random access iteration, right?! Since I haven't found any previous work in this direction on the web, I suspect there is a catch... (performance?) The only disadvantage I see, is that such iterators would have a rather different semantics from the std::(i)stream_iterator:s (equality etc), and that to create an end-iterator in the random access case, we cannot just rely on the default constructor, but need to pass in the stream, perhaps prepositioned at end. The original problem is to log/write data-records T on a persistant device, for which a std::stream(buf) interface is available. A reason for using the streambuf interface is that the log can support wrap-around functionality by simply using an intermediate streambuf that cares for necessary wrap-around functionality only, and otherwise just forwards transported characters to the device's streambuf. An enhanced std::istream_iterator would provide the log user with a convenient interface for reading records T out of the log. Thanks for any hints! /Mats
"Mats Eklund"
Hello,
does anybody know whether there is something fundamentally wrong with designing an istream iterator that conforms with the forward, or even random access, iterator concepts?
Nope; there's nothing wrong with it. It can get tricky, though: to meet the forward iterator requirements (operator* must return a true reference), your iterator would have to keep an internal cache of the element being referenced. -- Dave Abrahams Boost Consulting www.boost-consulting.com
On 10/16/05 3:01 PM, "Mats Eklund"
does anybody know whether there is something fundamentally wrong with designing an istream iterator that conforms with the forward, or even random access, iterator concepts? The std::istream_iterator conforms only with the simplest input iterator concept (stream need not support positioning). [TRUNCATE]
The virtual member functions given in std::basic_istream (and std::basic_streambuf) have enough functionality to map to iterator levels from simple input iterator up to random access. The functions are defined by default to be disabled or call simpler member functions in piecemeal. The child classes override these member functions to the appropriate level. However, there's no way for a piece of code to know how much functionality is actually present! Imagine the performance hit if your iterators used the stream-buffer like random access but the buffer only implemented up to simple input. The iterator library has to assume the worst case because it cannot tell what the actual case is. -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com
participants (3)
-
Daryle Walker
-
David Abrahams
-
Mats Eklund