Topic: Defect Report: io stream objects and static ctors [repost]
Author: "Ruslan Abdikeev" <darkgraf@hotmail.com>
Date: 9 Jul 2002 16:33:48 GMT Raw View
[ submitted to C++ Committee. -sdc ]
[My apologies for repost, but it seems like this article
didn't crossed a local server's boundary]
Problem:
Is it safe to use standard iostream objects from constructors
of static objects?
Are standard iostream objects constructed and are their
associations established at that time?
Surpisingly enough, Standard does NOT require that.
27.3/2 [lib.iostream.objects] guarantees that standard iostream
objects are constructed and their associations are established
before the body of main() begins execution.
It also refers to ios_base::Init class as the panacea for
constructors of static objects.
However, there's nothing in 27.3 [lib.iostream.objects],
in 27.4.2 [lib.ios.base], and in 27.4.2.1.6 [lib.ios::Init],
that would require implementations to allow access to standard
iostream objects from constructors of static objects.
Details:
Core text refers to some magic object ios_base::Init, which will
be discussed below:
"The [standard iostream] objects are constructed, and their
associations are established at some time prior to or during
first time an object of class basic_ios<charT,traits>::Init
is constructed, and in any case before the body of main
begins execution." (27.3/2 [lib.iostream.objects])
The first _non-normative_ footnote encourages implementations
to initialize standard iostream objects earlier than required.
However, the second _non-normative_ footnote makes
an explicit and unsupported claim:
"Constructors and destructors for static objects can access these
[standard iostream] objects to read input from stdin or write
output to
stdout or stderr." (27.3/2 footnote 265 [lib.iostream.objects])
The only bit of magic is related to that ios_base::Init class.
AFAIK, the rationale behind ios_base::Init was to bring an instance
of this class to each translation unit which #included <iostream> or
related header.
Such an inclusion would support the claim of footnote quoted above,
because in order to use some standard iostream object
it is necessary to #include <iostream>.
However, while Standard explicitly describes ios_base::Init as
an appropriate class for doing the trick, I failed to found a
mention of an _instance_ of ios_base::Init in Standard.
Proposed resolution:
At the end of header <iostream> synopsis in 27.3 [lib.iostream.objects]
namespace std
{
... extern istream cin; ...
add the following lines
namespace
{
ios_base::Init <some_implementation_defined_name>;
}
}
Kind regards,
Ruslan Abdikeev
email: ruslan@vr1.spb.ru
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
[ 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 ]