<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4039: &sect;[ostream.formatted.print]: Inappropriate usage of badbit in definition of 
vprint_unicode/vprint_nonunicode</title>
<meta property="og:title" content="Issue 4039: &sect;[ostream.formatted.print]: Inappropriate usage of badbit in definition of 
vprint_unicode/vprint_nonunicode">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4039.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#New">New</a> status.</em></p>
<h3 id="4039"><a href="lwg-active.html#4039">4039</a>. &sect;[ostream.formatted.print]: Inappropriate usage of <code>badbit</code> in definition of 
<code>vprint_unicode</code>/<code>vprint_nonunicode</code></h3>
<p><b>Section:</b> 31.7.6.3.5 <a href="https://wg21.link/ostream.formatted.print">[ostream.formatted.print]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jan Kokemüller <b>Opened:</b> 2024-01-13 <b>Last modified:</b> 2024-01-20</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#ostream.formatted.print">issues</a> in [ostream.formatted.print].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In section 31.7.6.3.5 <a href="https://wg21.link/ostream.formatted.print">[ostream.formatted.print]</a>, bullet 3.2 there is a mention of <code>badbit</code>:
</p>
<blockquote>
<p>
any exception thrown by the call to <code>vformat</code> is propagated without regard to the
value of <code>os.exceptions()</code> and without turning on <code>ios_base::badbit</code> in the error state of <code>os</code>.
</p>
</blockquote>
<p>
Now the affected functions <code>vprint_unicode</code> and <code>vprint_nonunicode</code> are specified to behave as 
formatted output functions (31.7.6.3.1 <a href="https://wg21.link/ostream.formatted.reqmts">[ostream.formatted.reqmts]</a>), which distinguishes two phases:
</p>
<ol>
<li><p>Output <em>generation</em>, which would call <code>setstate(ios_base::failbit)</code> in case of a failure
(which may throw an exception)</p></li>
<li><p>The actual output itself, which would call <code>setstate(ios_base::badbit)</code> in case of an exception</p></li>
</ol>
<p>
The <code>vformat</code> call is obviously part of the first phase (since it generates the output sequence and not
yet the actual output), and any failure here (such as an exception) would under regular formatted output function 
rules set <code>ios_base::failbit</code> and <em>not</em> <code>ios_base::badbit</code> (the latter would indicate a loss 
of the actual output sequence integrity and is therefore set by the second phase in case of an exception).
<p/>
Therefore mentioning <code>ios_base::badbit</code> in (3.2) doesn't really make sense, it could only meaningfully 
refer to <code>ios_base::failbit</code> instead.
</p>


<p id="res-4039"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4971" title=" Working Draft, Programming Languages — C++">N4971</a>.
</p>

<ol>
<li><p>Modify 31.7.6.3.5 <a href="https://wg21.link/ostream.formatted.print">[ostream.formatted.print]</a> as indicated:</p>

<blockquote>
<pre>
void vprint_unicode(ostream&amp; os, string_view fmt, format_args args);
void vprint_nonunicode(ostream&amp; os, string_view fmt, format_args args);
</pre>
<blockquote>
<p>
-3- <i>Effects</i>: Behaves as a formatted output function (31.7.6.3.1 <a href="https://wg21.link/ostream.formatted.reqmts">[ostream.formatted.reqmts]</a>) 
of <code>os</code>, except that:
</p>
<ol style="list-style-type: none">
<li><p>(3.1) &mdash; failure to generate output is reported as specified below, and</p></li>
<li><p>(3.2) &mdash; any exception thrown by the call to <code>vformat</code> is propagated without regard 
to the value of <code>os.exceptions()</code> and without turning on <code>ios_base::<ins>failbit</ins><del>badbit</del></code> 
in the error state of <code>os</code>.</p></li>
</ol>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
