<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3943: Clarify lifetime requirements of BasicFormatter and Formatter</title>
<meta property="og:title" content="Issue 3943: Clarify lifetime requirements of BasicFormatter and Formatter">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3943.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="3943"><a href="lwg-active.html#3943">3943</a>. Clarify lifetime requirements of <i>BasicFormatter</i> and <i>Formatter</i></h3>
<p><b>Section:</b> 28.5.6.3 <a href="https://wg21.link/format.formattable">[format.formattable]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Mark de Wever <b>Opened:</b> 2023-06-01 <b>Last modified:</b> 2023-06-08</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#format.formattable">active issues</a> in [format.formattable].</p>
<p><b>View all other</b> <a href="lwg-index.html#format.formattable">issues</a> in [format.formattable].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
A <a href="https://github.com/llvm/llvm-project/issues/62074">bug</a> has been
filed against libc++'s format implementation. The question arose whether
the parsed <i>chrono-specs</i> should still be available during executing the
formatter's <code>format</code> member function. Libc++'s implementation requires
this MSVC STL's implementation does not.
<p/>
It turns out there is a lifetime a requirement in the Standard, but it
would be good to clarify the intention of the wording. Prosed a small
wording improvement and a note to clarify the intention.
<p/>
Currently the <code>format</code> function may use elements in the range <code>[pc.begin(),
pc.end())</code> that have not been parsed. This does not seem to be in the
spirit of the formatting library. The wording change proposes to
restrict this range to the parsed range.
</p>

<p><i>[2023-06-08; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>
<p>
The proposed resolution is missing a guarantee that the parsed portion of the
range is unchanged between the calls to <code>parse</code> and
<code>format</code>. It must remain valid <em>and</em> unchanged.
</p>



<p id="res-3943"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4950" title=" Working Draft, Standard for Programming Language C++">N4950</a>.
</p>

<ol>
<li><p>Modify <i>BasicFormatter</i> requirements [tab:formatter.basic] as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 73: <i>BasicFormatter</i> requirements [tab:formatter.basic]</caption>
<tr>
<th align="center">Expression</th>
<th align="center">Return type</th>
<th align="center">Requirement</th>
</tr>

<tr>
<td colspan="3" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>f.format(u, fc)</code>
</td>
<td>
<code>FC::iterator</code>
</td>
<td>
Formats <code>u</code> according to the specifiers stored in<br/>
<code>*this</code>, writes the output to <code>fc.out()</code>, and returns<br/>
an iterator past the end of the output range.<br/>
The output shall only depend on <code>u</code>, <code>fc.locale()</code>,<br/>
<code>fc.arg(n)</code> for any value <code>n</code> of type <code>size_t</code>, and<br/>
the <ins>elements in the parsed</ins> range <ins>of</ins> <code>[pc.begin(), pc.end())</code><br/> 
from the last call to <code>f.parse(pc)</code>.
<p/>
<ins>[<i>Note ?</i>: Using elements in the parsed range of <code>[pc.begin(),<br/>
pc.end())</code> allows the formatter to store references, pointers, or<br/>
iterators to elements in the parsed range. For example,<br/>
<code>formatter&lt;chrono::day&gt;</code> might store the parsed <i>chrono-specs</i><br/>
(30.12 <a href="https://wg21.link/time.format">[time.format]</a>) in a <code>basic_string_view</code>. &mdash; <i>end note</i>]</ins>
</td>
</tr>

</table>
</blockquote>
</li>

<li><p>Modify <i>Formatter</i> requirements [tab:formatter] as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 74: <i>Formatter</i> requirements [tab:formatter]</caption>
<tr>
<th align="center">Expression</th>
<th align="center">Return type</th>
<th align="center">Requirement</th>
</tr>

<tr>
<td>
<code>f.format(t, fc)</code>
</td>
<td>
<code>FC::iterator</code>
</td>
<td>
Formats <code>t</code> according to the specifiers stored in<br/>
<code>*this</code>, writes the output to <code>fc.out()</code>, and returns<br/>
an iterator past the end of the output range.<br/>
The output shall only depend on <code>t</code>, <code>fc.locale()</code>,<br/>
<code>fc.arg(n)</code> for any value <code>n</code> of type <code>size_t</code>, and<br/>
the <ins>elements in the parsed</ins> range <ins>of</ins> <code>[pc.begin(), pc.end())</code><br/>
from the last call to <code>f.parse(pc)</code>.
<p/>
<ins>[<i>Note ?</i>: Using elements in the parsed range of <code>[pc.begin(),<br/>
pc.end())</code> allows the formatter to store references, pointers, or<br/>
iterators to elements in the parsed range. For example,<br/>
<code>formatter&lt;chrono::day&gt;</code> might store the parsed <i>chrono-specs</i><br/>
(30.12 <a href="https://wg21.link/time.format">[time.format]</a>) in a <code>basic_string_view</code>. &mdash; <i>end note</i>]</ins>
</td>
</tr>

<tr>
<td colspan="3" align="center">
<code>&hellip;</code>
</td>
</tr>

</table>
</blockquote>
</li>

</ol>






</body>
</html>
