Next: Exceptions
From: Chris Croughton on
Is it legal in C++ STL to assign or copy streams? For instance, if I
have:

#include <iostream>

int main()
{
std::ostream s(std::cout);
s << "Hello" << std::endl;
return 0;
}

is that valid? I'm using gcc versions 2.95 and 3.4.3, and both of them
give an error on the definition of s saying that the copy constructor
for ios_base is private (the same if I define s and then say s =
std:cout). Looking at the headers for both versions, the copy
constructor and operator=() are both explicitly private. I can't find
anything about this in the ISO spec., though, nor any reason for it. Is
this correct, or is it a bug (or misfeature) in the GCC implementation?

Chris C

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Maciej Sobczak on
Chris Croughton wrote:

> Is it legal in C++ STL to assign or copy streams?

What that would mean?
Think about positioning the stream, closing, overflowing, underflowing,
flushing, changing its buffer, locale, flags, etc. Which instance
(original, copy, both?) should be affected by these changes?

What should happen when copying fstreams? Should the new file be created?

What about slicing (like in your case)?
If you assign fstream to ostream (which is a base class), what should
happen to file-related state, like seek position?

And so on.

> I can't find
> anything about this in the ISO spec.

Yes, IOStreams are a bit under-specified.

> nor any reason for it.

The reason is that there is no sensible definition of copying w.r.t.
streams (and other resources or system-related "things" like threads or
processes, for example).

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: TonyO on
Chris Croughton wrote:
> Is it legal in C++ STL to assign or copy streams?

No. All stream classes ultimately derive from class std::ios_base,
which is defined in section 27.4.2 to have both a private
copy-constructor and a private copy-assignment operator.

> For instance, if I have:
>
> #include <iostream>
>
> int main()
> {
> std::ostream s(std::cout);
> s << "Hello" << std::endl;
> return 0;
> }
>
> is that valid?

No.

> I'm using gcc versions 2.95 and 3.4.3, and both of them
> give an error on the definition of s saying that the copy constructor
> for ios_base is private (the same if I define s and then say s =
> std:cout). Looking at the headers for both versions, the copy
> constructor and operator=() are both explicitly private. I can't find
> anything about this in the ISO spec., though, nor any reason for it. Is
> this correct, or is it a bug (or misfeature) in the GCC implementation?

These compilers are both correctly identifying the error. Look again
at section 27.4.2 of The Holy Standard.

But fear not, you have an alternative: stream classes can be
constructed that use an existing stream-buffer. This strategy can be
adopted to allow different streams to share the same I/O mechanism. I
guess you want this:

| #include <ostream>
| #include <iostream>
|
| int main()
| {
| std::ostream s(std::cout.rdbuf());
| s << "Hello" << std::endl;
| return 0;
| }

Hope this helps.
TonyO.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
In article <slrnd98scp.d71.chris(a)ccserver.keris.net>, Chris Croughton
<chris(a)keristor.net> writes
>Is it legal in C++ STL to assign or copy streams? For instance, if I
>have:
>
> #include <iostream>
>
> int main()
> {
> std::ostream s(std::cout);
> s << "Hello" << std::endl;
> return 0;
> }
>
>is that valid? I'm using gcc versions 2.95 and 3.4.3, and both of them
>give an error on the definition of s saying that the copy constructor
>for ios_base is private (the same if I define s and then say s =
>std:cout).

And they are both correct. Having more than one object trying to handle
the same data source/sink is a recipe for trouble.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: P.J. Plauger on
"Chris Croughton" <chris(a)keristor.net> wrote in message
news:slrnd98scp.d71.chris(a)ccserver.keris.net...

> Is it legal in C++ STL to assign or copy streams? For instance, if I
> have:
>
> #include <iostream>
>
> int main()
> {
> std::ostream s(std::cout);
> s << "Hello" << std::endl;
> return 0;
> }
>
> is that valid? I'm using gcc versions 2.95 and 3.4.3, and both of them
> give an error on the definition of s saying that the copy constructor
> for ios_base is private (the same if I define s and then say s =
> std:cout). Looking at the headers for both versions, the copy
> constructor and operator=() are both explicitly private. I can't find
> anything about this in the ISO spec., though, nor any reason for it. Is
> this correct, or is it a bug (or misfeature) in the GCC implementation?

It's correct. You can get (most of) what you want by initializing
the stream s to the same underlying stream buffer as cout, by
calling s.rdbuf(). That doesn't copy all the format state, etc., but
you can use copyfmt to get most of what's left.

It's certainly not impossible to make a stream copy constructible,
but the C++ committee nevertheless made a concerted decision to
disallow it.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 |  Next  |  Last
Pages: 1 2 3
Next: Exceptions