<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3636: formatter&lt;T&gt;::format should be const-qualified</title>
<meta property="og:title" content="Issue 3636: formatter&lt;T&gt;::format should be const-qualified">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3636.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++23">C++23</a> status.</em></p>
<h3 id="3636"><a href="lwg-defects.html#3636">3636</a>. <code>formatter&lt;T&gt;::format</code> should be <code>const</code>-qualified</h3>
<p><b>Section:</b> 28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Arthur O'Dwyer <b>Opened:</b> 2021-11-11 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>1
</p>
<p><b>View all other</b> <a href="lwg-index.html#formatter.requirements">issues</a> in [formatter.requirements].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In <a href="https://reviews.llvm.org/D112017">libc++ review</a>, we've noticed that we don't understand 
the implications of 28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a> bullet 3.1 and Table [tab:formatter.basic]: 
(emphasize mine):
</p>
<blockquote>
<p>
(3.1) &mdash; <code>f</code> is a <b>value</b> of type <code>F</code>,
<p/>
[&hellip;]
<p/>
Table 70: <i>BasicFormatter</i> requirements [tab:formatter.basic]
<p/>
[&hellip;]
<p/>
<code>f.parse(pc)</code> [<em>must compile</em>] [&hellip;]
<p/>
<code>f.format(u, fc)</code> [<em>must compile</em>] [&hellip;]
</p>
</blockquote>
<p>
According to Victor Zverovich, his intent was that <code>f.parse(pc)</code> should modify the 
state of <code>f</code>, but <code>f.format(u, fc)</code> should merely read <code>f</code>'s state to 
support format string compilation where formatter objects are immutable and therefore the 
<code>format</code> function must be <code>const</code>-qualified. 
<p/>
That is, a typical formatter should look something like this (modulo errors introduced by me):
</p>
<blockquote>
<pre>
struct WidgetFormatter {
  auto parse(std::format_parse_context&amp;) -&gt; std::format_parse_context::iterator;
  auto format(const Widget&amp;, std::format_context&amp;) const -&gt; std::format_context::iterator;
};
</pre>
</blockquote>
<p>
However, this is not reflected in the wording, which treats <code>parse</code> and <code>format</code> symmetrically.
Also, there is at least one example that shows a non-const <code>format</code> method:
</p>
<blockquote>
<pre>
template&lt;&gt; struct std::formatter&lt;color&gt; : std::formatter&lt;const char*&gt; {
  auto format(color c, format_context&amp; ctx) {
    return formatter&lt;const char*&gt;::format(color_names[c], ctx);
  }
};
</pre>
</blockquote>
<p>
Victor writes:
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<p>
Maybe we should [&hellip;] open an LWG issue clarifying that all standard formatters have a 
<code>const</code> format function.
</p>
</blockquote>
<p>
I'd like to be even more assertive: Let's open an LWG issue clarifying that all formatters must have a 
<code>const</code> format function!
</p>

<p><i>[2022-01-30; Reflector poll]</i></p>

<p>
Set priority to 1 after reflector poll.
</p>

<p><i>[2022-08-24 Approved unanimously in LWG telecon.]</i></p>


<p><i>[2022-11-12 Approved at November 2022 meeting in Kona. Status changed: Voting &rarr; WP.]</i></p>



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

<ol>

<li><p>Modify 28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> It might also be reasonable to do a drive-by clarification that when the Table 70 says 
"Stores the parsed format specifiers in <code>*this</code>," what it actually means is "Stores the parsed format 
specifiers in <code>g</code>." (But I don't think anyone's seriously confused by that wording.)
</p>
</blockquote>

<blockquote>
<p>
-3- Given character type <code>charT</code>, output iterator type <code>Out</code>, and formatting argument type <code>T</code>, 
in Table 70 and Table 71:
</p>
<ol style="list-style-type:none">
<li><p>(3.1) &mdash; <code>f</code> is a value of type <ins>(possibly <code>const</code>)</ins> <code>F</code>,</p></li>
<li><p><ins>(3.?) &mdash; <code>g</code> is an lvalue of type <code>F</code>,</ins></p></li>
<li><p>(3.2) &mdash; <code>u</code> is an lvalue of type <code>T</code>,</p></li>
<li><p>(3.3) &mdash; <code>t</code> is a value of a type convertible to (possibly <code>const</code>) <code>T</code>,</p></li>
<li><p>[&hellip;]</p></li>
</ol>
<p>
[&hellip;]
</p>
<blockquote>
<table border="1">
<caption>Table 70: <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><del>f</del><ins>g</ins>.parse(pc)</code>
</td>
<td>
<code>PC::iterator</code>
</td>
<td>
[&hellip;]<br/>
Stores the parsed format specifiers in <code>*this</code> and
returns an iterator past the end of the parsed range.
</td>
</tr>

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

</li>

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

<blockquote>
<p>
-6- An enabled specialization <code>formatter&lt;T, charT&gt;</code> meets the <i>BasicFormatter</i> requirements 
(28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a>).
<p/>
[<i>Example 1</i>:
</p>
<blockquote>
<pre>
#include &lt;format&gt;

enum color { red, green, blue };
const char* color_names[] = { "red", "green", "blue" };

template&lt;&gt; struct std::formatter&lt;color&gt; : std::formatter&lt;const char*&gt; {
  auto format(color c, format_context&amp; ctx) <ins>const</ins> {
    return formatter&lt;const char*&gt;::format(color_names[c], ctx);
  }
};

[&hellip;]
</pre>
</blockquote>
<p>
&mdash; <i>end example</i>]
</p>
</blockquote>
</li>

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

<blockquote>
<pre>
void advance_to(iterator it);
</pre>
<blockquote>
<p>
-8- <i>Effects:</i> Equivalent to: <code>out_ = std::move(it);</code>
<p/>
[<i>Example 1</i>:
</p>
<blockquote>
<pre>
struct S { int value; };

template&lt;&gt; struct std::formatter&lt;S&gt; {
  size_t width_arg_id = 0;
  
  <i>// Parses a width argument id in the format { digit }.</i>
  constexpr auto parse(format_parse_context&amp; ctx) {
    [&hellip;]
  }
  
  <i>// Formats an S with width given by the argument width_arg_id.</i>
  auto format(S s, format_context&amp; ctx) <ins>const</ins> {
    int width = visit_format_arg([](auto value) -&gt; int {
      if constexpr (!is_integral_v&lt;decltype(value)&gt;)
        throw format_error("width is not integral");
      else if (value &lt; 0 || value &gt; numeric_limits&lt;int&gt;::max())
        throw format_error("invalid width");
      else
        return value;
      }, ctx.arg(width_arg_id));
    return format_to(ctx.out(), "{0:x&lt;{1}}", s.value, width);
  }
};

[&hellip;]
</pre>
</blockquote>
<p>
&mdash; <i>end example</i>]
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
template&lt;class Duration, class charT&gt;
struct formatter&lt;chrono::<i>local-time-format-t</i>&lt;Duration&gt;, charT&gt;;
</pre>
<blockquote>
<p>
-15- Let <code>f</code> be [&hellip;]
<p/>
-16- <i>Remarks:</i> [&hellip;]
</p>
</blockquote>
<blockquote>
<pre>
template&lt;class Duration, class TimeZonePtr, class charT&gt;
struct formatter&lt;chrono::zoned_time&lt;Duration, TimeZonePtr&gt;, charT&gt;
  : formatter&lt;chrono::<i>local-time-format-t</i>&lt;Duration&gt;, charT&gt; {
  template&lt;class FormatContext&gt;
    typename FormatContext::iterator
      format(const chrono::zoned_time&lt;Duration, TimeZonePtr&gt;&amp; tp, FormatContext&amp; ctx) <ins>const</ins>;
};
</pre>
</blockquote>
<pre>
template&lt;class FormatContext&gt;
  typename FormatContext::iterator
    format(const chrono::zoned_time&lt;Duration, TimeZonePtr&gt;&amp; tp, FormatContext&amp; ctx) <ins>const</ins>;
</pre>
<blockquote>
<p>
-17- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
sys_info info = tp.get_info();
return formatter&lt;chrono::<i>local-time-format-t</i>&lt;Duration&gt;, charT&gt;::
         format({tp.get_local_time(), &amp;info.abbrev, &amp;info.offset}, ctx);
</pre></blockquote>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
