Boost logo

Boost :

From: Hans Dembinski (hans.dembinski_at_[hidden])
Date: 2019-10-07 08:14:46

Hi Roberto,

> On 3. Oct 2019, at 14:22, Roberto Hinz via Boost <boost_at_[hidden]> wrote:
> Hi all, this is a continuation of the thread
> "A solution to return dynamic strings without heap allocation. Any
> interest?"
> Just telling you that I rewrote the docs, especially the rationale:
> Best regards
> Robhz

Quoted from the rationale:

"Your function is complex to use. The user needs to implement a class that derives from ostream to customize the destination. It’s a complex task for most C++ programmers."

Agreed, although boost.iostreams makes that easier.

"It’s impossible to achieve a good perfomance. std::ostream does not provide direct access to the buffer. to_base64 needs to call member functions like write or put for every little piece of the content, or to use an itermediate buffer."

It is not impossible to achieve good performance, page 68 of list problems, which are solvable.

In practice, increasing the buffer size helps and turning off synchronisation with stdio:
The SO answer lists several examples were C++'s iostreams beats C's stdio in performance.

Your argument is also not convincing. Just calling member functions doesn't make something slow if you compile with optimisations, which is a must with C++. I think it is quite natural that the stream makes it hard for you to touch the buffer. The stream objects hide buffer management under an interface. The ostream object handles the buffer for you, you don't have to know when you hit the boundary and things need to be flushed to the device. You can't hide something and expose it at the same time, this is breaking the encapsulation, so naturally, the streams make it difficult to touch the buffer directly. Although you can, if you really want to, and it is pretty simple to set up:

char Buffer[N];
std::ofstream file("file.txt");
file.rdbuf()->pubsetbuf(Buffer, N);

Now you can mess around with the stack-allocated buffer. It is not clear to me what the advantage of outbuf is over this.

I think the real problem with iostreams is that it lacks good documentation and tutorials on how to do the more complicated things.

Best regards,

Boost list run by bdawes at, gregod at, cpdaniel at, john at