Topic: iostream lib, copy constructors and operator=


Author: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1997/07/03
Raw View
Hi,

unfortunately I haven't seen any reply to this question. I'm not sure
whether I can give a correct answer but I hope so..

Howard E. Hinnant (heh@beamtech.com) wrote:
: I have only scanned section 27 (Input/output library), but I can't find
: any mention of copy constructors or assignment operators on stream
: objects.  Elsewhere the standard states:

:   17.2.2.2  Functions within classes      [lib.functions.within.classes]

: 1 For  the  sake  of  exposition, Clauses _lib.language.support_ through
:   _lib.input.output_ do not describe copy constructors, assignment oper-
:   ators,  or  (non-virtual) destructors with the same apparent semantics
:   as those that can be generated by default (_class.ctor_, _class.dtor_,
:   _class.copy_).

: This would seem to imply that the following program is legal.

: #include <iostream>

: void main()
: {
:    ostream a(cout);
:    a << "Hi\n";
: }

I don't think this is legal! The above section explicitly refers to the
description. However, since the IOStream classes make no mention of
copy-constructors and assignment operators at all. Thus, I think these
are not defined at all. Unfortunately, I haven't found a quote which
explicitly says that this indeed the correct logic... I can only come
to this conclusion by comparision with other classes which definitely
have a copy constructor: For example, in the class definition of
'deque' a copy constructor is listed but the behavior is later not
described.

In any case, copy construction and assignment don't make much sense and
are inaccessible as they should in several C++ libraries. Maybe someone
else can give better details...
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I'm seeking a new employment - See my homepage for details
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: heh@beamtech.com (Howard E. Hinnant)
Date: 1997/06/23
Raw View
I have only scanned section 27 (Input/output library), but I can't find
any mention of copy constructors or assignment operators on stream
objects.  Elsewhere the standard states:

  17.2.2.2  Functions within classes      [lib.functions.within.classes]

1 For  the  sake  of  exposition, Clauses _lib.language.support_ through
  _lib.input.output_ do not describe copy constructors, assignment oper-
  ators,  or  (non-virtual) destructors with the same apparent semantics
  as those that can be generated by default (_class.ctor_, _class.dtor_,
  _class.copy_).

This would seem to imply that the following program is legal.

#include <iostream>

void main()
{
   ostream a(cout);
   a << "Hi\n";
}

If so, I have more questions:

What happens if you pass an ofstream to a function by value.  When the
temp argument in the function goes out of scope, it's destructor should
close the stream.  Does the copy of the stream in the calling program
remain open or closed after the function call?

void stupid(ostream os) {
   os << "Hi\n";
}

int main() {
   ofstream os("stuff");
   stupid(os);
   os << " There\n";  // is the stream still open?
}

This all boils down to how the stream classes are implemented.  Do they
use ref-counted pointers to the system resources?  How do these pointers
behave under copy and assignment?

I can live with almost any answer to these questions, but I think that the
standard should say something.

Perhaps I have missed this section.  If so, could someone please point me
in the right direction?  Perhaps I'm simply ignorant of obvious details
figured out long ago.  I would appreciate some enlightenment.

Thanks,
Howard
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]