<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1123: No requirement that standard streams be flushed</title>
<meta property="og:title" content="Issue 1123: No requirement that standard streams be flushed">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1123.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++11">C++11</a> status.</em></p>
<h3 id="1123"><a href="lwg-defects.html#1123">1123</a>. No requirement that standard streams be flushed</h3>
<p><b>Section:</b> 31.5.2.2.6 <a href="https://wg21.link/ios.init">[ios.init]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> James Kanze <b>Opened:</b> 2009-05-14 <b>Last modified:</b> 2021-06-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#ios.init">issues</a> in [ios.init].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
As currently formulated, the standard doesn't require that there
is ever a flush of <code>cout</code>, etc.  (This implies, for example, that
the classical hello, world program may have no output.)  In the
current draft
(<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf">N2798</a>),
there is a requirement that the objects
be constructed before <code>main</code>, and before the dynamic
initialization of any non-local objects defined after the
inclusion of <code>&lt;iostream&gt;</code> in the same translation unit.  The only
requirement that I can find concerning flushing, however, is in
 [ios::Init], where the destructor of the last
<code>std::ios_base::Init</code> object flushes.  But there is, as far as I
can see, no guarantee that such an object ever exists.
</p>
<p>
Also, the wording in  [iostreams.objects] says that:
</p>
<blockquote><p>
The objects
are constructed and the associations are established at some
time prior to or during the first time an object of class
<code>ios_base::Init</code> is constructed, and in any case before the body
of main begins execution.
</p></blockquote>
<p>
In  [ios::Init], however, as an
effect of the constructor, it says that
</p>
<blockquote><p>
If <code>init_cnt</code> is zero,
the function stores the value one in <code>init_cnt</code>, then constructs
and initializes the objects <code>cin</code>, <code>cout</code>, <code>cerr</code>, <code>clog</code>
<code>wcin</code>, <code>wcout</code>, <code>wcerr</code>, and <code>wclog</code>"
</p></blockquote>

<p>
which seems to forbid earlier
construction.
</p>

<p>
(Note that with these changes, the exposition only "<code>static
int init_cnt</code>" in <code>ios_base::Init</code> can be dropped.)
</p>
<p>
Of course, a determined programmer can still inhibit the
flush with things like:
</p>
<blockquote><pre>
new std::ios_base::Init ;       //  never deleted
</pre></blockquote>
<p>
or (in a function):
</p>
<blockquote><pre>
std::ios_base::Init ensureConstruction ;
//  ...
exit( EXIT_SUCCESS ) ;
</pre></blockquote>
<p>
Perhaps some words somewhere to the effect that all
<code>std::ios_base::Init</code> objects should have static lifetime
would be in order.
</p>

<p><i>[
2009 Santa Cruz:
]</i></p>


<blockquote><p>
Moved to Ready.  Some editorial changes are expected (in addition to the
proposed wording) to remove <code>init_cnt</code> from <code>Init</code>.
</p></blockquote>



<p id="res-1123"><b>Proposed resolution:</b></p>
<p>
Change 31.4 <a href="https://wg21.link/iostream.objects">[iostream.objects]</a>/2:
</p>

<blockquote><p>
-2- The objects are constructed and the associations are established at
some time prior to or during the first time an object of class
<code>ios_base::Init</code> is constructed, and in any case before the body
of main begins execution.<sup>292</sup> The objects are not destroyed
during program execution.<sup>293</sup>
<del>If a translation unit includes
<code>&lt;iostream&gt;</code> or explicitly constructs an
<code>ios_base::Init</code> object, these stream objects shall be
constructed before dynamic initialization of non-local objects defined
later in that translation unit.</del>
<ins>The results of including <code>&lt;iostream&gt;</code> in a translation
unit shall be as if <code>&lt;iostream&gt;</code> defined an instance of
<code>ios_base::Init</code> with static lifetime. Similarly, the entire
program shall behave as if there were at least one instance of
<code>ios_base::Init</code> with static lifetime.</ins>
</p></blockquote>

<p>
Change  [ios::Init]/3:
</p>

<blockquote>
<pre>
Init();
</pre>
<blockquote><p>
-3- <i>Effects:</i> Constructs an object of class <code>Init</code>.
<del>If <code>init_cnt</code> is zero, the function stores the value one in
<code>init_cnt</code>, then constructs and initializes the objects
<code>cin</code>, <code>cout</code>, <code>cerr</code>, <code>clog</code> (27.4.1),
<code>wcin</code>, <code>wcout</code>, <code>wcerr</code>, and <code>wclog</code>
(27.4.2). In any case, the function then adds one to the value stored in
<code>init_cnt</code>.</del>
<ins>Constructs and initializes the objects <code>cin</code>, <code>cout</code>,
<code>cerr</code>, <code>clog</code>, <code>wcin</code>, <code>wcout</code>,
<code>wcerr</code> and <code>wclog</code> if they have not already been
constructed and initialized.</ins>
</p></blockquote>
</blockquote>

<p>
Change  [ios::Init]/4:
</p>

<blockquote>
<pre>
~Init();
</pre>
<blockquote><p>
-4- <i>Effects:</i> Destroys an object of class <code>Init</code>.
<del>The function subtracts one from the value stored in <code>init_cnt</code> and,
if the resulting stored value is one,</del>
<ins>If there are no other instances of the class still in
existance,</ins>
calls <code>cout.flush()</code>,
<code>cerr.flush()</code>, <code>clog.flush()</code>, <code>wcout.flush()</code>,
<code>wcerr.flush()</code>, <code>wclog.flush()</code>.
</p></blockquote>
</blockquote>






</body>
</html>
