<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4146: &sect;[format.formatter.spec]/3 unconditionally enables nonlocking for container adaptors</title>
<meta property="og:title" content="Issue 4146: &sect;[format.formatter.spec]/3 unconditionally enables nonlocking for container adaptors">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4146.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="4146"><a href="lwg-active.html#4146">4146</a>. &sect;[format.formatter.spec]/3 unconditionally enables nonlocking for container adaptors</h3>
<p><b>Section:</b> 28.5.6.4 <a href="https://wg21.link/format.formatter.spec">[format.formatter.spec]</a>, 23.6.13 <a href="https://wg21.link/container.adaptors.format">[container.adaptors.format]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2024-08-31 <b>Last modified:</b> 2024-09-01</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#format.formatter.spec">active issues</a> in [format.formatter.spec].</p>
<p><b>View all other</b> <a href="lwg-index.html#format.formatter.spec">issues</a> in [format.formatter.spec].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
28.5.6.4 <a href="https://wg21.link/format.formatter.spec">[format.formatter.spec]</a>/3 says that the library provides a specialization of 
<code class='backtick'>enable_nonlocking_formatter_optimization</code> with value <code class='backtick'>true</code> corresponding to each library-provided 
specialization of <code class='backtick'>formatter</code>, unless otherwise specified. Although it actually states 
"for each type <code class='backtick'>T</code>", the intent is that partial specializations are also provided corresponding to 
library-provided partial specializations of formatter.
<p/>
23.6.13 <a href="https://wg21.link/container.adaptors.format">[container.adaptors.format]</a>/1 says the library provides a partial specialization of 
<code class='backtick'>formatter</code> for each of the container adaptor templates <code class='backtick'>priority_queue</code>, <code class='backtick'>queue</code>, and <code class='backtick'>stack</code>. 
Together with 28.5.6.4 <a href="https://wg21.link/format.formatter.spec">[format.formatter.spec]</a>/3, that means that e.g. 
<code>enable_nonlocking_formatter_optimization&lt;stack&lt;T&gt;&gt; == true</code>. Formatting a stack of 
that type will enable the nonlocking optimization even if 
<code>enable_nonlocking_formatter_optimization&lt;T&gt; == false</code>. To avoid this, the author of <code class='backtick'>T</code> 
must partially specialize <code class='backtick'>enable_nonlocking_formatter_optimization</code> to <code class='backtick'>false</code> for all container 
adaptors when they adapt a container of <code class='backtick'>T</code>.
<p/>
It is clearly not the design intent that programmers must explicitly opt out of the nonlocking 
optimization, so this is a defect that LWG should correct. Since <a href="https://wg21.link/P3235R3" title=" std::print more types faster with less memory">P3235R3</a> was applied 
as a Defect Report to C++23, the resolution of this issue should be so applied as well.
<p/>
<b>Suggested Resolution:</b>
<p/>
LEWG was reticent to apply the optimization to general ranges &mdash; ostensibly due to the possibility 
of deadlock in program-defined iterator operations &mdash; but apparently unconcerned about the iterators 
of program-defined containers nor the fancy pointers of program-defined allocators. It seems consistent 
with that design to ignore the "container" part of "container adaptors" and only pay attention to the 
elements that are going to be formatted. (I have prototyped this resolution on MSVCSTL, albeit slightly 
modified since neither MSVC nor Clang like this partial specialization form.)
</p>


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

<ol>
<li><p>Modify 23.6.13 <a href="https://wg21.link/container.adaptors.format">[container.adaptors.format]</a> as indicated:</p>

<blockquote>
<p>
-1- For each of <code>queue</code>, <code>priority_queue</code>, and <code>stack</code>, the library provides the 
following <del>formatter</del> specialization<ins>s</ins> where <code><i>adaptor-type</i></code> is the 
name of the template:
</p>
<blockquote><pre>
namespace std {
  template&lt;class charT, class T, formattable&lt;charT&gt; Container, class... U&gt;
  struct formatter&lt;<i>adaptor-type</i>&lt;T, Container, U...&gt;, charT&gt; {
    [&hellip;]
  };

  <ins>template&lt;class T, class Container, class... U&gt;</ins>
  <ins>constexpr bool enable_nonlocking_formatter_optimization&lt;<i>adaptor-type</i>&lt;T, Container, U...&gt;&gt; =</ins>
    <ins>enable_nonlocking_formatter_optimization&lt;T&gt;;</ins>
}
</pre></blockquote>
</blockquote>
</li>

</ol>






</body>
</html>
