<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3718: P2418R2 broke the overload resolution for std::basic_format_arg</title>
<meta property="og:title" content="Issue 3718: P2418R2 broke the overload resolution for std::basic_format_arg">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3718.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="3718"><a href="lwg-defects.html#3718">3718</a>. P2418R2 broke the overload resolution for <code>std::basic_format_arg</code></h3>
<p><b>Section:</b> 28.5.8.1 <a href="https://wg21.link/format.arg">[format.arg]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2022-06-17 <b>Last modified:</b> 2023-03-23</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#format.arg">issues</a> in [format.arg].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
While correcting some bugs in MSVC STL,
<a href="https://github.com/microsoft/STL/pull/2768#discussion_r892950656">it is found that</a>
<a href="https://wg21.link/P2418R2" title=" Add support for std::generator-like types to std::format">P2418R2</a> broke the overload resolution involving non-const lvalue: constructing
<code>basic_format_arg</code> from a non-<code>const basic_string</code> lvalue incorrectly selects the
<code>T&amp;&amp;</code> overload and uses <code>handle</code>, while the separated <code>basic_string</code>
overload should be selected (i.e. the old behavior before P2418R2 did the right thing). Currently
MSVC STL is using a workaround that treats <code>basic_string</code> to be as if passed by value
during overload resolution.
<p/>
I think the <code>T&amp;&amp;</code> overload should not interfere the old result of overload resolution,
which means that when a type is <code>const</code>-formattable, the newly added non-<code>const</code>
mechanism shouldn't be considered.
</p>

<p><i>[2022-07-06; Reflector poll]</i></p>

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

<p><i>[2022-10-19; Would be resolved by <a href="lwg-defects.html#3631" title="basic_format_arg(T&amp;&amp;) should use remove_cvref_t&lt;T&gt; throughout (Status: C++23)">3631</a><sup><a href="https://cplusplus.github.io/LWG/issue3631" title="Latest snapshot">(i)</a></sup>]</i></p>


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

<p>
This wording is relative to <a href="https://wg21.link/N4910" title=" Working Draft, Standard for Programming Language C++">N4910</a>.
</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> The below presented wording adds back the <code>const T&amp;</code> constructor and its specification
that were present before <a href="https://wg21.link/P2418R2" title=" Add support for std::generator-like types to std::format">P2418R2</a>. Furthermore it adds an additional constraint to the <code>T&amp;&amp;</code>
constructor and simplifies the <i>Effects</i> of this constructors to the <code>handle</code> construction case.]
</p>
</blockquote>

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

<blockquote>
<blockquote>
<pre>
namespace std {
  template&lt;class Context&gt;
  class basic_format_arg {
  public:
    class handle;

  private:
    [&hellip;]
    template&lt;class T&gt; explicit basic_format_arg(T&amp;&amp; v) noexcept; <i>// exposition only</i>
    <ins>template&lt;class T&gt; explicit basic_format_arg(const T&amp; v) noexcept; <i>// exposition only</i></ins>
    [&hellip;]
  };
}
</pre>
</blockquote>
[&hellip;]
<pre>
template&lt;class T&gt; explicit basic_format_arg(T&amp;&amp; v) noexcept;
</pre>
<blockquote>
<p>
-4- <i>Constraints</i>: The template specialization
</p>
<blockquote><pre>
typename Context::template formatter_type&lt;remove_cvref_t&lt;T&gt;&gt;
</pre></blockquote>
<p>
meets the <i>BasicFormatter</i> requirements (28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a>). The extent
to which an implementation determines that the specialization meets the <i>BasicFormatter</i>
requirements is unspecified, except that as a minimum the expression
</p>
<blockquote><pre>
typename Context::template formatter_type&lt;remove_cvref_t&lt;T&gt;&gt;()
  .format(declval&lt;T&amp;&gt;(), declval&lt;Context&amp;&gt;())
</pre></blockquote>
<p>
shall be well-formed when treated as an unevaluated operand (7.2.3 <a href="https://wg21.link/expr.context">[expr.context]</a>)<ins>, and
if this overload were not declared, the overload resolution  would find no usable candidate or be
ambiguous. [<i>Note ?</i>: This overload has no effect if the overload resolution among other
overloads succeeds. &mdash; <i> end note</i>]</ins>.
<p/>
-5- <i>Effects</i>:
</p>
<ol style="list-style-type:none">
<li><p><del>(5.1) &mdash; if <code>T</code> is <code>bool</code> or <code>char_type</code>, initializes <code>value</code>
with <code>v</code>;</del></p></li>
<li><p><del>(5.2) &mdash; otherwise, if <code>T</code> is <code>char</code> and <code>char_type</code> is <code>wchar_t</code>,
initializes <code>value</code> with <code>static_cast&lt;wchar_t&gt;(v)</code>;</del></p></li>
<li><p><del>(5.3) &mdash; otherwise, if <code>T</code> is a signed integer type (6.9.2 <a href="https://wg21.link/basic.fundamental">[basic.fundamental]</a>)
and <code>sizeof(T) &lt;= sizeof(int)</code>, initializes <code>value</code> with <code>static_cast&lt;int&gt;(v)</code>;</del></p></li>
<li><p><del>(5.4) &mdash; otherwise, if <code>T</code> is an unsigned integer type and <code>sizeof(T) &lt;=
sizeof(unsigned int)</code>, initializes <code>value</code> with <code>static_cast&lt;unsigned int&gt;(v)</code>;</del></p></li>
<li><p><del>(5.5) &mdash; otherwise, if <code>T</code> is a signed integer type and <code>sizeof(T) &lt;=
sizeof(long long int)</code>, initializes <code>value</code> with <code>static_cast&lt;long long int&gt;(v)</code>;</del></p></li>
<li><p><del>(5.6) &mdash; otherwise, if <code>T</code> is an unsigned integer type and <code>sizeof(T) &lt;=
sizeof(unsigned long long int)</code>, initializes <code>value</code> with <code>static_cast&lt;unsigned long long int&gt;(v)</code>;</del></p></li>
<li><p><del>(5.7) &mdash; otherwise, i</del><ins>I</ins>nitializes <code>value</code> with <code>handle(v)</code>.</p></li>
</ol>
</blockquote>
<pre>
<ins>template&lt;class T&gt; explicit basic_format_arg(const T&amp; v) noexcept;</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Constraints</i>: The template specialization</ins>
</p>
<blockquote><pre>
<ins>typename Context::template formatter_type&lt;T&gt;</ins>
</pre></blockquote>
<p>
<ins>meets the <i>Formatter</i> requirements (28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a>). The extent
to which an implementation determines that the specialization meets the <i>Formatter</i>
requirements is unspecified, except that as a minimum the expression</ins>
</p>
<blockquote><pre>
<ins>typename Context::template formatter_type&lt;T&gt;()
  .format(declval&lt;const T&amp;&gt;(), declval&lt;Context&amp;&gt;())</ins>
</pre></blockquote>
<p>
<ins>shall be well-formed when treated as an unevaluated operand (7.2.3 <a href="https://wg21.link/expr.context">[expr.context]</a>).</ins>
<p/>
<ins>-?- <i>Effects</i>:</ins>
</p>
<ol style="list-style-type:none">
<li><p><ins>(?.1) &mdash; if <code>T</code> is <code>bool</code> or <code>char_type</code>, initializes <code>value</code>
with <code>v</code>;</ins></p></li>
<li><p><ins>(?.2) &mdash; otherwise, if <code>T</code> is <code>char</code> and <code>char_type</code> is <code>wchar_t</code>,
initializes <code>value</code> with <code>static_cast&lt;wchar_t&gt;(v)</code>;</ins></p></li>
<li><p><ins>(?.3) &mdash; otherwise, if <code>T</code> is a signed integer type (6.9.2 <a href="https://wg21.link/basic.fundamental">[basic.fundamental]</a>)
and <code>sizeof(T) &lt;= sizeof(int)</code>, initializes <code>value</code> with <code>static_cast&lt;int&gt;(v)</code>;</ins></p></li>
<li><p><ins>(?.4) &mdash; otherwise, if <code>T</code> is an unsigned integer type and <code>sizeof(T) &lt;=
sizeof(unsigned int)</code>, initializes <code>value</code> with <code>static_cast&lt;unsigned int&gt;(v)</code>;</ins></p></li>
<li><p><ins>(?.5) &mdash; otherwise, if <code>T</code> is a signed integer type and <code>sizeof(T) &lt;=
sizeof(long long int)</code>, initializes <code>value</code> with <code>static_cast&lt;long long int&gt;(v)</code>;</ins></p></li>
<li><p><ins>(?.6) &mdash; otherwise, if <code>T</code> is an unsigned integer type and <code>sizeof(T) &lt;=
sizeof(unsigned long long int)</code>, initializes <code>value</code> with <code>static_cast&lt;unsigned long long int&gt;(v)</code>;</ins></p></li>
<li><p><ins>(?.7) &mdash; otherwise, initializes <code>value</code> with <code>handle(v)</code>.</ins></p></li>
</ol>
</blockquote>

</blockquote>

</li>

</ol>
</blockquote>

<p><i>[2023-03-22 Resolved by the adoption of <a href="lwg-defects.html#3631" title="basic_format_arg(T&amp;&amp;) should use remove_cvref_t&lt;T&gt; throughout (Status: C++23)">3631</a><sup><a href="https://cplusplus.github.io/LWG/issue3631" title="Latest snapshot">(i)</a></sup> in Issaquah. Status changed: New &rarr; Resolved.]</i></p>



<p id="res-3718"><b>Proposed resolution:</b></p>





</body>
</html>
