Topic: Assignment of iostreams
Author: Gabriel Sanchez Gutierrez <gsg@sema.es>
Date: 1995/12/30 Raw View
Hello,
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.
Any hint about how to perform such assignment using standard things?
Thanks and happy new year.
--------------------------------------------------------------------------
Gabriel Sanchez Gutierrez | Phone: +34.1.327.28.28
DIS - Sema Group sae | Fax: +34.1.754.32.52
Albarracin, 25 | Email: gsg@sema.es
28037 Madrid - SPAIN | WWW: http://www.sema.es/~gsg
---
[ 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. ]
Author: Per Bothner <bothner@cygnus.com>
Date: 1995/12/31 Raw View
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. ]