[ ... ]
Of course, you can provide a quote or reference to the place
the standard guarantees the creation of at least one such
object?
Sort of, in 27.3/2. That's what actually creates cin, cout,
etc. -- so without it, the problem isn't going to be lack of
output from the program -- it's going to be extra output from
the linker saying your program has undefined externals, and
then you'll get no executable at all.
That's what triggers the construction of cin, cout, etc. It's
not what defines the symbols.
For various reasons, an implementation must do some sort of
funny stuff with the standard stream objects: they can't be
simply variables at namespace scope, with the given type, unless
the implementation has some mechanism to ensure that they are
constructed before (or during) the construction of the first
ios_base::Init object, and that thy are the very last object to
be destructed. Thus, g++ declares them as raw memory
(char[ sizeof( ostream ) ], etc., using a compiler extension to
ensure alignment); ios_base::Init uses placement new to
construct them. (I suspect that this is probably the most
widespread solution.)
And if you try outputting before any ios_base::Init objects are
constructed, you really do get undefined behavior. According to
§27.3: "The objects are constructed and the associations are
established at some time prior to or during the first time an
object of class ios_base::Init is constructed, and in any case
before the body of main begins execution." But Alf is right
that there is no guarantee that any ios_base::Init objects are
ever constructed, and thus, that the streams will ever be
flushed. I'm sure this is a defect; I'll raise the issue in
comp.std.c++.
(In practice, all of the compilers I know ensure the
construction before main by simply defining an instance of
ios_base::Init somewhere in the library, where it will be
constructed before main. Also, for a variety of historical
reasons, all of the implementations I know define a static
What about it? Do you mean: "Is all output that has been
written to an fstream required to be written to the external
file?" If so, the answer is basically yes.
~std::basic_filebuf() calls close() (§27.8.1.2/3). Close
flushes the output by calling overflow(EOF) if a put area
exists (§27.8.1.3/6). Of course, this assumes normal exit --
if the program exits via abort() (for one example) buffers
aren't flushed.
Also, if you do something like:
std:

stream* p = new std:

fstream( "filename" ) ;
and never delete p, the buffers won't be flushed. Or if you do
something like:
int
main()
{
std:

fstream out( "filename" ) ;
// ...
exit( 0 ) ;
}
In general, however, I would consider it an error to output to a
file without explicitly closing it, then testing the error
status, and reporting a write error somehow. For command line
programs, the return status should be EXIT_FAILURE, or some
implementation specific value which represents failure, in such
cases, probably with an error message on cerr. For a GUI, I'd
expect some sort of pop-up error message. For a server,
whatever the server is supposed to do in case of a serious
error---under Unix, syslog, and maybe an email to someone.