Topic: Is calling wcout.rdbuf(...) allowed?


Author: cbarron3@ix.netcom.com (Carl Barron)
Date: Sat, 15 Oct 2005 04:50:16 GMT
Raw View
Christoph Schulz <kristov@arcor.de> wrote:

> Hello,
>
> I wonder if it is allowed to change the stream buffer of a predefined
> stream (e.g. cout, cin, wcout, etc.) *without* resetting it to the
> original one at program end. Consider the following program snippet:
>
>
> #include <iostream>
> #include <streambuf>
> using namespace std;
> class wsimplestreambuf : public wstreambuf
> {
> //... conformant implementation ...
> };
>
> int main ()
> {
>   static wsimplestreambuf buf;
>   wcout.rdbuf(&buf);  // <-- is this legal C++?
> }
>
   its legal, but unsafe as wcout outlives main() that said
a 'stack class' that holds a reference/pointer to the wostream and
the old streambuf *, on construction and on destruction restores
the old stream buffer pointer to the stream. is a better idea

int main()
{
    wsimplestreambuf buf;
    hold h(wcout); // save ptr/ref to wcout,and its wstreambuf *
    wcout.rdbuf(&buf);
    //... use wcout

} // on exit h's dtor called and it restores the old wstreambuf * to
wcout.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kristov@arcor.de (Christoph Schulz)
Date: Thu, 6 Oct 2005 14:29:45 GMT
Raw View
Hello,

I wonder if it is allowed to change the stream buffer of a predefined
stream (e.g. cout, cin, wcout, etc.) *without* resetting it to the
original one at program end. Consider the following program snippet:


#include <iostream>
#include <streambuf>
using namespace std;
class wsimplestreambuf : public wstreambuf
{
//... conformant implementation ...
};

int main ()
{
  static wsimplestreambuf buf;
  wcout.rdbuf(&buf);  // <-- is this legal C++?
}


Is this legal C++? Would it be legal if "buf" were dynamically
allocated? I didn't find anything in the current standard that addresses
these questions.

At least with one implementation the program crashed if main() exited
with a different associated stream buffer in wcout. I think (but I
didn't check) that wcout's destructor tried to delete the associated
buffer which was not dynamically allocated. (Yes, I know that 27.3p2
says "The objects are not destroyed during program execution." But does
that mean that wcout's destructor is never called? And makes this one
sentence the program above legal?)

Regards,
  Christoph Schulz

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: wade@stoner.com
Date: Fri, 7 Oct 2005 21:12:28 CST
Raw View
Christoph Schulz wrote:
> int main ()
> {
>   static wsimplestreambuf buf;
>   wcout.rdbuf(&buf);  // <-- is this legal C++?
> }
>
>
> Is this legal C++? Would it be legal if "buf" were dynamically
> allocated? I didn't find anything in the current standard that addresses
> these questions.

Some implementations flush wcout after user static objects are
destroyed.  I don't know if that is legal, but it certainly happens.
In general, 'buf' will not be the last object destructed, and if the
destructor of any other object uses wcout, this code will be a mess.

I'd expect this to work if buf were on the heap and never destroyed.
An additional requirement would be that wsimplestreambuf still be
usable that late in the program (it better not reference any objects
with static lifetimes).

HTH

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]