<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3881: Incorrect formatting of container adapters backed by std::string</title>
<meta property="og:title" content="Issue 3881: Incorrect formatting of container adapters backed by std::string">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3881.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="3881"><a href="lwg-defects.html#3881">3881</a>. Incorrect formatting of container adapters backed by <code>std::string</code></h3>
<p><b>Section:</b> 23.6.13 <a href="https://wg21.link/container.adaptors.format">[container.adaptors.format]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Victor Zverovich <b>Opened:</b> 2023-02-10 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>Not Prioritized
</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>
According to 23.6.13 <a href="https://wg21.link/container.adaptors.format">[container.adaptors.format]</a> container adapters such as <code>std::stack</code> are 
formatted by forwarding to the underlying container:
</p>
<blockquote><pre>
template&lt;class FormatContext&gt;
  typename FormatContext::iterator
    format(<i>maybe-const-adaptor</i>&amp; r, FormatContext&amp; ctx) const;
</pre>
<blockquote>
<p>
<i>Effects</i>: Equivalent to: <code>return <i>underlying_</i>.format(r.c, ctx);</code>
</p>
</blockquote>
</blockquote>
<p>
This gives expected results for <code>std::stack&lt;T&gt;</code> and most types of underlying container:
</p>
<blockquote><pre>
auto s = std::format("{}", std::stack(std::deque{'a', 'b', 'c'}));
// s == "['a', 'b', 'c']"
</pre></blockquote>
<p>
However, when the underlying container is <code>std::string</code> the output is:
</p>
<blockquote><pre>
auto s = std::format("{}", std::stack{std::string{"abc"}});
// s == "abc"
</pre></blockquote>
<p>
This is clearly incorrect because <code>std::stack</code> itself is not a string (it is only backed by a string) 
and inconsistent with formatting of ranges where non-string range types are formatted as comma-separated values 
delimited by <code>'['</code> and <code>']'</code>. The correct output in this case would be <code>['a', 'b', 'c']</code>.
<p/>
Here is an illustration of this issue on godbolt using {fmt} and an implementation of the formatter for container 
adapters based on the one from the standard: <a href="https://godbolt.org/z/P1nrM1986">https://godbolt.org/z/P1nrM1986</a>.
<p/>
A simple fix is to wrap the underlying container in <code>std::views::all(_t)</code> 
(<a href="https://godbolt.org/z/8MT1be838">https://godbolt.org/z/8MT1be838</a>).
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">


<p>
This wording is relative to <a href="https://wg21.link/N4928" title=" Working Draft, Standard for Programming Language C++">N4928</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 formatter specialization
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; {
  private:
    using <i>maybe-const-adaptor</i> =                       // <i>exposition only</i>
      <i>fmt-maybe-const</i>&lt;<i>adaptor-type</i>&lt;T, Container, U...&gt;, charT&gt;;
    formatter&lt;<ins>views::all_t&lt;const</ins> Container<ins>&amp;&gt;</ins>, charT&gt; <i>underlying_</i>;          // <i>exposition only</i>

  public:
    template&lt;class ParseContext&gt;
      constexpr typename ParseContext::iterator
        parse(ParseContext&amp; ctx);

    template&lt;class FormatContext&gt;
      typename FormatContext::iterator
        format(<i>maybe-const-adaptor</i>&amp; r, FormatContext&amp; ctx) const;
  };
}
</pre>
</blockquote>
[&hellip;]
<pre>
template&lt;class FormatContext&gt;
  typename FormatContext::iterator
    format(<i>maybe-const-adaptor</i>&amp; r, FormatContext&amp; ctx) const;
</pre>
<blockquote>
<p>
-3- <i>Effects</i>: Equivalent to: <code>return <i>underlying_</i>.format(<ins>views::all(</ins>r.c<ins>)</ins>, ctx);</code>
</p>
</blockquote>
</blockquote>

</li>

</ol>
</blockquote>

<p><i>[2023-02-10 Tim provides updated wording]</i></p>

<p>The container elements may not be const-formattable so we cannot 
use the <code>const</code> formatter unconditionally. Also the current wording 
is broken because an adaptor is not range and we cannot use 
<code><i>fmt-maybe-const</i></code> on the adaptor &mdash; only the underlying container.</p>

<p><i>[Issaquah 2023-02-10; LWG issue processing]</i></p>

<p>Move to Immediate for C++23</p>

<p><i>[2023-02-13 Approved at February 2023 meeting in Issaquah. Status changed: Immediate &rarr; WP.]</i></p>



<p id="res-3881"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4928" title=" Working Draft, Standard for Programming Language C++">N4928</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 formatter specialization
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; {
  private:
    <ins>using <i>maybe-const-container</i> =                     // <i>exposition only</i>
      <i>fmt-maybe-const</i>&lt;Container, charT&gt;;</ins>
    using <i>maybe-const-adaptor</i> =                       // <i>exposition only</i>
      <i><del>fmt-</del>maybe-const</i>&lt;<ins>is_const_v&lt;<i>maybe-const-container</i>&gt;, </ins><i>adaptor-type</i>&lt;T, Container, U...&gt;<del>, charT</del>&gt;; <ins>// <i>see 25.2 <a href="https://wg21.link/ranges.syn">[ranges.syn]</a></i></ins>
      
    formatter&lt;<ins>ranges::ref_view&lt;<i>maybe-const-container</i>&gt;</ins><del>Container</del>, charT&gt; <i>underlying_</i>;          // <i>exposition only</i>

  public:
    template&lt;class ParseContext&gt;
      constexpr typename ParseContext::iterator
        parse(ParseContext&amp; ctx);

    template&lt;class FormatContext&gt;
      typename FormatContext::iterator
        format(<i>maybe-const-adaptor</i>&amp; r, FormatContext&amp; ctx) const;
  };
}
</pre>
</blockquote>
[&hellip;]
<pre>
template&lt;class FormatContext&gt;
  typename FormatContext::iterator
    format(<i>maybe-const-adaptor</i>&amp; r, FormatContext&amp; ctx) const;
</pre>
<blockquote>
<p>
-3- <i>Effects</i>: Equivalent to: <code>return <i>underlying_</i>.format(r.c, ctx);</code>
</p>
</blockquote>
</blockquote>

</li>

</ol>





</body>
</html>
