<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3340: Formatting functions should throw on argument/format string mismatch in &sect;[format.functions]</title>
<meta property="og:title" content="Issue 3340: Formatting functions should throw on argument/format string mismatch in &sect;[format.functions]">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3340.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++20">C++20</a> status.</em></p>
<h3 id="3340"><a href="lwg-defects.html#3340">3340</a>. Formatting functions should throw on argument/format string mismatch in &sect;[format.functions]</h3>
<p><b>Section:</b> 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Great Britain <b>Opened:</b> 2019-11-17 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#format.functions">issues</a> in [format.functions].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses <a href="https://github.com/cplusplus/nbballot/issues/226">GB 229</a></b></p>

<p>
Formatting functions don't allow throwing on incorrect arguments. 
<code>std::format</code> is only allowed to throw if <code>fmt</code> is not a format string, but the 
intention is it also throws for errors during formatting, e.g. there are fewer arguments than 
required by the format string.
</p>
<p>
Proposed change:
</p>
<p>
Allow exceptions even when the format string is valid. Possibly state the <i>Effects:</i> more
precisely.
</p>
<p>
<b>Victor Zverovich:</b>
<p/>
LEWG approved resolution of this NB comment as an LWG issue.
</p>

<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4835">N4835</a>.</p>

<blockquote class="note">
<p>
[<i>Drafting Note:</i> Depending on whether LWG <a href="lwg-defects.html#3336" title="How does std::vformat handle exception thrown by formatters? (Status: Resolved)">3336</a><sup><a href="https://cplusplus.github.io/LWG/issue3336" title="Latest snapshot">(i)</a></sup>'s wording has been accepted when 
this issue's wording has been accepted, two mutually exclusive options are prepared, depicted below 
by <b>Option A</b> and <b>Option B</b>, respectively.]
</p>
</blockquote>

<p><b>Option A</b> (LWG <a href="lwg-defects.html#3336" title="How does std::vformat handle exception thrown by formatters? (Status: Resolved)">3336</a><sup><a href="https://cplusplus.github.io/LWG/issue3336" title="Latest snapshot">(i)</a></sup> has been accepted)</p>

<ol>
<li><p>Change 28.5.2.1 <a href="https://wg21.link/format.string.general">[format.string.general]</a> as follows:</p>

<blockquote>
<p>
-1- A <i>format string</i> <ins>for arguments <code>args</code></ins> is a (possibly empty) sequence of 
<i>replacement fields</i>, <i>escape sequences</i>, and characters other than <code>{</code> and <code>}</code>. 
[&hellip;]
<p/>
-2- The <i>arg-id</i> field specifies the index of the argument in <code>args</code> whose value is to be 
formatted and inserted into the output instead of the replacement field. <ins>If there is no argument 
with the index <i>arg-id</i> in <code>args</code>, the string is not a format string.</ins> The optional 
<i>format-specifier</i> field explicitly specifies a format for the replacement value.
<p/>
[&hellip;]
<p/>
-5- The <i>format-spec</i> field contains <i>format specifications</i> that define how the value 
should be presented. Each type can define its own interpretation of the <i>format-spec</i> field. 
<ins>If <i>format-spec</i> doesn't conform to the format specifications for the argument in <code>args</code> 
referred to by <i>arg-id</i>, the string is not a format string.</ins> [&hellip;]
</p>
</blockquote>
</li>

<li><p>Before 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a> insert a new sub-clause as indicated:</p>

<blockquote>
<p>
<ins><b>20.20.? Error reporting [format.err.report]</b></ins>
<p/>
<ins>-?- Formatting functions throw exceptions to report formatting and other errors. They throw 
<code>format_error</code> if an argument <code>fmt</code> is passed that is not a format string for arguments 
<code>args</code> and propagate exceptions thrown by <code>formatter</code> specializations and iterator operations. 
Failure to allocate storage is reported by throwing an exception as described in 
16.4.6.14 <a href="https://wg21.link/res.on.exception.handling">[res.on.exception.handling]</a>.</ins>
</p>
</blockquote>
</li>

<li><p>Modify 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a> as indicated:</p>

<blockquote>
<pre>
string vformat(string_view fmt, format_args args);
wstring vformat(wstring_view fmt, wformat_args args);
string vformat(const locale&amp; loc, string_view fmt, format_args args);
wstring vformat(const locale&amp; loc, wstring_view fmt, wformat_args args);
</pre>
<blockquote>
<p>
-6- [&hellip;]
<p/>
-7- <i>Throws:</i> <del><code>format_error</code> if <code>fmt</code> is not a format string</del><ins>As specified in 28.5.3 <a href="https://wg21.link/format.err.report">[format.err.report]</a></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class Out&gt;
  Out vformat_to(Out out, string_view fmt, format_args_t&lt;Out, char&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, wstring_view fmt, format_args_t&lt;Out, wchar_t&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, const locale&amp; loc, string_view fmt,
                 format_args_t&lt;Out, char&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, const locale&amp; loc, wstring_view fmt,
                 format_args_t&lt;Out, wchar_t&gt; args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-15- <i>Throws:</i> <del><code>format_error</code> if <code>fmt</code> is not a format string</del><ins>As specified in 28.5.3 <a href="https://wg21.link/format.err.report">[format.err.report]</a></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      string_view fmt, const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      wstring_view fmt, const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      const locale&amp; loc, string_view fmt,
                                      const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      const locale&amp; loc, wstring_view fmt,
                                      const Args&amp;... args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-21- <i>Throws:</i> <del><code>format_error</code> if <code>fmt</code> is not a format string</del><ins>As specified in 28.5.3 <a href="https://wg21.link/format.err.report">[format.err.report]</a></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class... Args&gt;
  size_t formatted_size(string_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(wstring_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(const locale&amp; loc, string_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(const locale&amp; loc, wstring_view fmt, const Args&amp;... args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-25- <i>Throws:</i> <del><code>format_error</code> if <code>fmt</code> is not a format string</del><ins>As specified in 28.5.3 <a href="https://wg21.link/format.err.report">[format.err.report]</a></ins>.
</p>
</blockquote>
</blockquote>
</li>
</ol>

<p><b>Option B</b> (LWG <a href="lwg-defects.html#3336" title="How does std::vformat handle exception thrown by formatters? (Status: Resolved)">3336</a><sup><a href="https://cplusplus.github.io/LWG/issue3336" title="Latest snapshot">(i)</a></sup> has <b>not</b> been accepted)</p>

<ol>
<li><p>Change 28.5.2.1 <a href="https://wg21.link/format.string.general">[format.string.general]</a> as follows:</p>

<blockquote>
<p>
-1- A <i>format string</i> <ins>for arguments <code>args</code></ins> is a (possibly empty) sequence of 
<i>replacement fields</i>, <i>escape sequences</i>, and characters other than <code>{</code> and <code>}</code>. 
[&hellip;]
<p/>
-2- The <i>arg-id</i> field specifies the index of the argument in <code>args</code> whose value is to be 
formatted and inserted into the output instead of the replacement field. <ins>If there is no argument 
with the index <i>arg-id</i> in <code>args</code>, the string is not a format string.</ins> The optional 
<i>format-specifier</i> field explicitly specifies a format for the replacement value.
<p/>
[&hellip;]
<p/>
-5- The <i>format-spec</i> field contains <i>format specifications</i> that define how the value 
should be presented. Each type can define its own interpretation of the <i>format-spec</i> field. 
<ins>If <i>format-spec</i> doesn't conform to the format specifications for the argument in <code>args</code> 
referred to by <i>arg-id</i>, the string is not a format string.</ins> [&hellip;]
</p>
</blockquote>
</li>

<li><p>Modify 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a> as indicated:</p>

<blockquote>
<pre>
string vformat(string_view fmt, format_args args);
wstring vformat(wstring_view fmt, wformat_args args);
string vformat(const locale&amp; loc, string_view fmt, format_args args);
wstring vformat(const locale&amp; loc, wstring_view fmt, wformat_args args);
</pre>
<blockquote>
<p>
-6- [&hellip;]
<p/>
-7- <i>Throws:</i> <code>format_error</code> if <code>fmt</code> is not a format string <ins>for <code>args</code></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class Out&gt;
  Out vformat_to(Out out, string_view fmt, format_args_t&lt;Out, char&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, wstring_view fmt, format_args_t&lt;Out, wchar_t&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, const locale&amp; loc, string_view fmt,
                 format_args_t&lt;Out, char&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, const locale&amp; loc, wstring_view fmt,
                 format_args_t&lt;Out, wchar_t&gt; args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-15- <i>Throws:</i> <code>format_error</code> if <code>fmt</code> is not a format string <ins>for <code>args</code></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      string_view fmt, const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      wstring_view fmt, const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      const locale&amp; loc, string_view fmt,
                                      const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      const locale&amp; loc, wstring_view fmt,
                                      const Args&amp;... args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-21- <i>Throws:</i> <code>format_error</code> if <code>fmt</code> is not a format string <ins>for <code>args</code></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class... Args&gt;
  size_t formatted_size(string_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(wstring_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(const locale&amp; loc, string_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(const locale&amp; loc, wstring_view fmt, const Args&amp;... args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-25- <i>Throws:</i> <code>format_error</code> if <code>fmt</code> is not a format string <ins>for <code>args</code></ins>.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2020-02-12, Prague; LWG discussion]</i></p>

<p>
Option A is the only one we look at to resolve LWG <a href="lwg-defects.html#3336" title="How does std::vformat handle exception thrown by formatters? (Status: Resolved)">3336</a><sup><a href="https://cplusplus.github.io/LWG/issue3336" title="Latest snapshot">(i)</a></sup> as well. During the discussions some 
wording refinements have been suggested that are integrated below.
</p>


<p id="res-3340"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4849">N4849</a>.</p>

<ol>
<li><p>Change 28.5.2.1 <a href="https://wg21.link/format.string.general">[format.string.general]</a> as follows:</p>

<blockquote>
<p>
-1- A <i>format string</i> <ins>for arguments <code>args</code></ins> is a (possibly empty) sequence of 
<i>replacement fields</i>, <i>escape sequences</i>, and characters other than <code>{</code> and <code>}</code>. 
[&hellip;]
<p/>
-2- The <i>arg-id</i> field specifies the index of the argument in <code>args</code> whose value is to be 
formatted and inserted into the output instead of the replacement field. <ins>If there is no argument 
with the index <i>arg-id</i> in <code>args</code>, the string is not a format string for <code>args</code>.</ins> The optional 
<i>format-specifier</i> field explicitly specifies a format for the replacement value.
<p/>
[&hellip;]
<p/>
-5- The <i>format-spec</i> field contains <i>format specifications</i> that define how the value 
should be presented. Each type can define its own interpretation of the <i>format-spec</i> field. 
<ins>If <i>format-spec</i> does not conform to the format specifications for the argument type referred to 
by <i>arg-id</i>, the string is not a format string for <code>args</code>.</ins> [&hellip;]
</p>
</blockquote>
</li>

<li><p>Before 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a> insert a new sub-clause as indicated:</p>

<blockquote>
<p>
<ins><b>20.20.? Error reporting [format.err.report]</b></ins>
<p/>
<ins>-?- Formatting functions throw <code>format_error</code> if an argument <code>fmt</code> is passed 
that is not a format string for <code>args</code>. They propagate exceptions thrown by operations of
<code>formatter</code> specializations and iterators. Failure to allocate storage is 
reported by throwing an exception as described in 16.4.6.14 <a href="https://wg21.link/res.on.exception.handling">[res.on.exception.handling]</a>.</ins>
</p>
</blockquote>
</li>

<li><p>Modify 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a> as indicated:</p>

<blockquote>
<pre>
string vformat(string_view fmt, format_args args);
wstring vformat(wstring_view fmt, wformat_args args);
string vformat(const locale&amp; loc, string_view fmt, format_args args);
wstring vformat(const locale&amp; loc, wstring_view fmt, wformat_args args);
</pre>
<blockquote>
<p>
-6- [&hellip;]
<p/>
-7- <i>Throws:</i> <del><code>format_error</code> if <code>fmt</code> is not a format string</del><ins>As specified in 28.5.3 <a href="https://wg21.link/format.err.report">[format.err.report]</a></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class Out&gt;
  Out vformat_to(Out out, string_view fmt, format_args_t&lt;Out, char&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, wstring_view fmt, format_args_t&lt;Out, wchar_t&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, const locale&amp; loc, string_view fmt,
                 format_args_t&lt;Out, char&gt; args);
template&lt;class Out&gt;
  Out vformat_to(Out out, const locale&amp; loc, wstring_view fmt,
                 format_args_t&lt;Out, wchar_t&gt; args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-15- <i>Throws:</i> <del><code>format_error</code> if <code>fmt</code> is not a format string</del><ins>As specified in 28.5.3 <a href="https://wg21.link/format.err.report">[format.err.report]</a></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      string_view fmt, const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      wstring_view fmt, const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      const locale&amp; loc, string_view fmt,
                                      const Args&amp;... args);
template&lt;class Out, class... Args&gt;
  format_to_n_result&lt;Out&gt; format_to_n(Out out, iter_difference_t&lt;Out&gt; n,
                                      const locale&amp; loc, wstring_view fmt,
                                      const Args&amp;... args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-21- <i>Throws:</i> <del><code>format_error</code> if <code>fmt</code> is not a format string</del><ins>As specified in 28.5.3 <a href="https://wg21.link/format.err.report">[format.err.report]</a></ins>.
</p>
</blockquote>
[&hellip;]
<pre>
template&lt;class... Args&gt;
  size_t formatted_size(string_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(wstring_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(const locale&amp; loc, string_view fmt, const Args&amp;... args);
template&lt;class... Args&gt;
  size_t formatted_size(const locale&amp; loc, wstring_view fmt, const Args&amp;... args);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-25- <i>Throws:</i> <del><code>format_error</code> if <code>fmt</code> is not a format string</del><ins>As specified in 28.5.3 <a href="https://wg21.link/format.err.report">[format.err.report]</a></ins>.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
