You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@stdcxx.apache.org by Misha Aizatulin <av...@hot.ee> on 2008/11/15 07:01:26 UTC

Documentation example doesn't compile with gcc

hi all,

  at the very end of http://stdcxx.apache.org/doc/stdlibug/34-2.html
there is an example of using a reference to create a "copy" of cout.
However this example doesn't initialize the defined reference at once
(which is required in C++) and for this reason doesn't compile with my
gcc 4.2.1.

  For convenience here is the example:

int main(int argc, char *argv[])
{
  std::ostream& fr;
  if (argc > 1)
    fr = *(new std::ofstream(argv[1]));
  else
    fr = std::cout;

  fr << "Hello world!" << std::endl;

  if (&fr!=&std::cout)
    delete(&fr);
}

Cheers,
  Misha

Re: Documentation example doesn't compile with gcc

Posted by Martin Sebor <ms...@gmail.com>.
Misha Aizatulin wrote:
> hi all,
> 
>   at the very end of http://stdcxx.apache.org/doc/stdlibug/34-2.html
> there is an example of using a reference to create a "copy" of cout.
> However this example doesn't initialize the defined reference at once
> (which is required in C++) and for this reason doesn't compile with my
> gcc 4.2.1.

That's definitely incorrect. The uninitialized reference is not
C++ and as noted in LWG issue 50, stream objects are not copyable.
The next C++ Standard makes that explicit by declaring the copy
ctor and copy assignment operator of class ios_base deleted.

I opened STDCXX-1024 to have the problematic example removed:
   http://issues.apache.org/jira/browse/STDCXX-1024

LWG issue #50 for reference
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#50

Thanks for pointing it out!
Martin

> 
>   For convenience here is the example:
> 
> int main(int argc, char *argv[])
> {
>   std::ostream& fr;
>   if (argc > 1)
>     fr = *(new std::ofstream(argv[1]));
>   else
>     fr = std::cout;
> 
>   fr << "Hello world!" << std::endl;
> 
>   if (&fr!=&std::cout)
>     delete(&fr);
> }
> 
> Cheers,
>   Misha
>