Article View: comp.std.c++
Article #2969Re: Assignment of iostreams
From: Per Bothner
Date: Sun, 31 Dec 1995 01:23
Date: Sun, 31 Dec 1995 01:23
70 lines
3642 bytes
3642 bytes
In article <4c1af7$brh@vivaldi.sema.es>,
>In my code there were some lines trying to assign an ostream to
>another ostream, using operator= (that seems to be defined by
>default).
>
>I have upgraded my compiler (to GCC2.7.2) and now that is not possible.
>Looking at the .h files, I have found a class called _IO_ostream_withassign
>that defines such operator.
>
>I am sure this class is not standard. In fact, I have not found references
>to it in the standard text.
It is "de facto" standard, in the sense that it is "traditional" and
provided by most/all implementations that strive for compatibility
with the original iostreams from AT&T C++ 2.x. I resisted implementing
it for GNU C++, but finally gave in.
The Draft Standard provides a standard way to achieve the effect you want.
This is not implemented in GNU iostreams - nor many other implementations.
So you can be either standards-conforming or (mostly) portable,
but not both. But before you try either, read this section from
the GNU C++ FAQ (maintained by Joe Buck):
Why can't I assign one stream to another?
=========================================
[ Thanks to Per Bothner and Jerry Schwarz for this section. ]
Assigning one stream to another seems like a reasonable thing to do,
but it's a bad idea. Usually, this comes up because people want to
assign to `cout'. This is poor style, especially for libraries, and is
contrary to good object-oriented design. (Libraries that write directly
to `cout' are less flexible, modular, and object-oriented).
The iostream classes do not allow assigning to arbitrary streams,
because this can violate typing:
ifstream foo ("foo");
istrstream str(...);
foo = str;
foo->close (); /* Oops! Not defined for istrstream! */
The original cfront implementation of iostreams by Jerry Schwarz
allows you to assign to `cin', `cout', `cerr', and `clog', but this is
not part of the draft standard for iostreams and generally isn't
considered a good idea, so standard-conforming code shouldn't use this
technique.
The GNU implementation of iostream did not support assigning to
`cin', `cout', `cerr', and `clog' for quite a while, but it now does,
for backward compatibility with cfront iostream (versions 2.6.1 and
later of libg++).
The ANSI/ISO C++ Working Paper does provide ways of changing the
streambuf associated with a stream. Assignment isn't allowed; there is
an explicit named member that must be used.
However, it is not wise to do this, and the results are confusing.
For example: `fstream::rdbuf' is supposed to return the *original*
filebuf, not the one you assigned. (This is not yet implemented in GNU
iostream.) This must be so because `fstream::rdbuf' is defined to
return a `filebuf *'.
--Per Bothner
Cygnus Support bothner@cygnus.com
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<199512310015.QAA29054@cygnus.com>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<4c1af7$brh@vivaldi.sema.es>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 31 Dec 95 01:23:06 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Message-ID: <199512310015.QAA29054@cygnus.com> Subject: Re: Assignment of iostreams References: <4c1af7$brh@vivaldi.sema.es> Organization: Cygnus Support, Mountain View, CA X-Original-Date: Sat, 30 Dec 1995 16:15:22 -0800 From: Per Bothner <bothner@cygnus.com> X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMOXmGeEDnX0m9pzZAQHBbgF/UjcSOTi7rSlMpLHvuUZetBLI80aa4Ojh +jAaFZgQ+sIfj91NhwWolzbF60vM2cOI =51tY