Dietmar Kuehl <> wrote:
> Marcus Kwok wrote:
>> Dietmar Kuehl <> wrote:
>>> Use 'std:
stream old(0);' instead: the constructor of 'std:
stream'
>>> requires a stream buffer but this can be null.
>>
>> Nice, this seems to work great. Thanks.
>
> Note, however, this creation of a stream object is relatively
> involved. At the very least it initialized quite a few member
> variables and it might even do memory allocations. Thus, if you
> need to save the format frequently you might want to consider
> using just one stream object, e.g. by using a function static
> object:
>
> {
> static std:
stream old(0);
> ...
> }
>
> This object will only be constructed once. Of course, it will
> also cause problems in multi-threaded code.
Oh, I see. So, for example, if I had a
std::vector<Data> v;
and did a
std::copy(v.begin(), v.end(), std:

stream_iterator<Data>(std::cout, "\n"));
then it would have to create/destroy a new std:

stream for every
element in the vector, correct? That does seem like quite a bit of
overhead.
The static member/multithreading issue is also significant, but since
that is OT here (until they add concurrency to the standard) I can defer
that argument to another place and time.
It would be nice if the standard had a lightweight "format" class to
store this in.
This question was mainly academic for me, and due to the two issues
above, it looks like it may not be possible to have a completely
satisfactory solution that can be used in production code. I guess I
should just stick to explicitly formatting where needed.
--
Marcus Kwok