<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3336: How does std::vformat handle exception thrown by formatters?</title>
<meta property="og:title" content="Issue 3336: How does std::vformat handle exception thrown by formatters?">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3336.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#Resolved">Resolved</a> status.</em></p>
<h3 id="3336"><a href="lwg-defects.html#3336">3336</a>. How does <code>std::vformat</code> handle exception thrown by formatters?</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#Resolved">Resolved</a>
 <b>Submitter:</b> Tam S. B. <b>Opened:</b> 2019-11-11 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>2
</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#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The specification for <code>std::vformat</code> in 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a>/7 says
</p>
<blockquote>
<p>
<i>Throws:</i> <code>format_error</code> if <code>fmt</code> is not a format string.
</p>
</blockquote>
<p>
It seems unclear whether <code>vformat</code> throws when an exception is thrown by the <code>formatter</code>, 
e.g. when the format string is valid, but the corresponding argument cannot be formatted with the given 
format string.
<p/>
For example, the <code>"c"</code> format specifier is specified to throw <code>format_error</code> if the 
corresponding argument is not in the range of representable values for <code>charT</code> 
(Table 60 [tab:format.type.int]). It seems unclear whether <code>vformat</code> propagates this exception.
<p/>
It also appears unclear whether <code>vformat</code> may throw other types of exception (e.g. <code>bad_alloc</code>) 
when the formatter or the constructor of <code>std::string</code> result throws.
</p>

<p><i>[2019-11-20 Issue Prioritization]</i></p>

<p>Priority to 2 after reflector discussion.</p>

<p><i>[2019-11-20; Victor Zverovich provides initial wording]</i></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> LWG <a href="lwg-defects.html#3340" title="Formatting functions should throw on argument/format string mismatch in &sect;[format.functions] (Status: C++20)">3340</a><sup><a href="https://cplusplus.github.io/LWG/issue3340" title="Latest snapshot">(i)</a></sup> has considerable wording overlap with this issue.
If 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> is applied at the same meeting or later than LWG <a href="lwg-defects.html#3340" title="Formatting functions should throw on argument/format string mismatch in &sect;[format.functions] (Status: C++20)">3340</a><sup><a href="https://cplusplus.github.io/LWG/issue3340" title="Latest snapshot">(i)</a></sup>, 
please refer to <b>Option A</b> in LWG <a href="lwg-defects.html#3340" title="Formatting functions should throw on argument/format string mismatch in &sect;[format.functions] (Status: C++20)">3340</a><sup><a href="https://cplusplus.github.io/LWG/issue3340" title="Latest snapshot">(i)</a></sup> as a guideline how to apply the merge.]
</p>
</blockquote>

<ol>
<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 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>

</blockquote>

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

<p>
Resolved by LWG <a href="lwg-defects.html#3340" title="Formatting functions should throw on argument/format string mismatch in &sect;[format.functions] (Status: C++20)">3340</a><sup><a href="https://cplusplus.github.io/LWG/issue3340" title="Latest snapshot">(i)</a></sup>.
</p>


<p id="res-3336"><b>Proposed resolution:</b></p>
<p>
Resolved by LWG <a href="lwg-defects.html#3340" title="Formatting functions should throw on argument/format string mismatch in &sect;[format.functions] (Status: C++20)">3340</a><sup><a href="https://cplusplus.github.io/LWG/issue3340" title="Latest snapshot">(i)</a></sup>.
</p>




</body>
</html>
