<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3720: Restrict the valid types of arg-id for width and precision in std-format-spec</title>
<meta property="og:title" content="Issue 3720: Restrict the valid types of arg-id for width and precision in std-format-spec">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3720.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="3720"><a href="lwg-defects.html#3720">3720</a>. Restrict the valid types of <i>arg-id</i> for <i>width</i> and <i>precision</i> in <i>std-format-spec</i></h3>
<p><b>Section:</b> 28.5.2.2 <a href="https://wg21.link/format.string.std">[format.string.std]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Mark de Wever <b>Opened:</b> 2022-06-19 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#format.string.std">active issues</a> in [format.string.std].</p>
<p><b>View all other</b> <a href="lwg-index.html#format.string.std">issues</a> in [format.string.std].</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>
Per 28.5.2.2 <a href="https://wg21.link/format.string.std">[format.string.std]</a>/7
</p>
<blockquote><p>
If <code>{ <i>arg-id<sub>opt</sub></i> }</code> is used in a <i>width</i> or <i>precision</i>, 
the value of the corresponding formatting argument is used in its place. If the
corresponding formatting argument is not of integral type, or its value is negative for 
<i>precision</i> or non-positive for <i>width</i>, an exception of type <code>format_error</code> 
is thrown.
</p></blockquote>
<p>
The issue is the integral type requirement. The following code is currently valid:
</p>
<blockquote><pre>
std::cout &lt;&lt; std::format("{:*^{}}\n", 'a', '0');
std::cout &lt;&lt; std::format("{:*^{}}\n", 'a', true);
</pre></blockquote>
<p>
The output of the first example depends on the value of <code>'0'</code> in the implementation. 
When a <code>char</code> has <code>signed char</code> as underlying type negative values are invalid, 
while the same value would be valid when the underlying type is <code>unsigned char</code>.
For the second example the range of a boolean is very small, so this seems not really useful.
<p/>
Currently libc++ rejects these two examples and MSVC STL accepts them.
The members of the MSVC STL team, I spoke, agree these two cases should be rejected.
<p/>
The following integral types are rejected by both libc++ and MSVC STL:
</p>
<blockquote><pre>
std::cout &lt;&lt; std::format("{:*^{}}\n", 'a', L'0');
std::cout &lt;&lt; std::format("{:*^{}}\n", 'a', u'0');
std::cout &lt;&lt; std::format("{:*^{}}\n", 'a', U'0');
std::cout &lt;&lt; std::format("{:*^{}}\n", 'a', u8'0');
</pre></blockquote>
<p>
In order to accept these character types they need to meet the basic formatter 
requirements per 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a>/20 and 28.5.5 <a href="https://wg21.link/format.functions">[format.functions]</a>/25
</p>
<blockquote><p>
<code>formatter&lt;remove_cvref_t&lt;T<sub><i>i</i></sub>&gt;, charT&gt;</code> meets the 
<i>BasicFormatter</i> requirements (28.5.6.1 <a href="https://wg21.link/formatter.requirements">[formatter.requirements]</a>) for each 
<code>T<sub><i>i</i></sub></code> in <code>Args</code>.
</p></blockquote>
<p>
which requires adding the following enabled formatter specializations to
28.5.6.4 <a href="https://wg21.link/format.formatter.spec">[format.formatter.spec]</a>.
</p>
<blockquote><pre>
template&lt;&gt; struct formatter&lt;wchar_t, char&gt;;

template&lt;&gt; struct formatter&lt;char8_t, charT&gt;;
template&lt;&gt; struct formatter&lt;char16_t, charT&gt;;
template&lt;&gt; struct formatter&lt;char32_t, charT&gt;;
</pre></blockquote>
<p>
Note, that the specialization <code>template&lt;&gt; struct formatter&lt;char, wchar_t&gt;</code>
is already required by the Standard.
<p/>
Not only do they need to be added, but it also needs to be specified how
they behave when their value is not in the range of representable values
for <code>charT</code>.
<p/>
Instead of requiring these specializations, I propose to go the other
direction and limit the allowed types to signed and unsigned integers.
</p>

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

<p>
Set priority to 2 after reflector poll. Tim Song commented:
</p>
<blockquote><p>
"This is technically a breaking change, so we should do it sooner rather than later.
</p><p>
"I don't agree with the second part of the argument though - I don't see how this wording requires adding those transcoding specializations. Nothing in this wording requires integral types that cannot be packed into basic_format_arg to be accepted.
</p><p>
"I also think we need to restrict this to signed or unsigned integer types with size no greater than sizeof(long long). Larger types get type-erased into a handle and the value isn't really recoverable without heroics."
</p></blockquote>

<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>

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

<blockquote>
<p>
-7- If <code>{ <i>arg-id<sub>opt</sub></i> }</code> is used in a <i>width</i> or <i>precision</i>, 
the value of the corresponding formatting argument is used in its place. If the corresponding 
formatting argument is not of <del>integral</del><ins>signed or unsigned integer</ins> type, 
or its value is negative for <i>precision</i> or non-positive for <i>width</i>, an exception of 
type <code>format_error</code> is thrown.
</p>
</blockquote>
</li>

<li><p>Add a new paragraph to C.2.10 <a href="https://wg21.link/diff.cpp20.utilities">[diff.cpp20.utilities]</a> as indicated:</p>

<blockquote>
<p>
<ins><b>Affected subclause:</b> 28.5 <a href="https://wg21.link/format">[format]</a></ins>
<p/>
<ins><b>Change:</b> Requirement changes of <i>arg-id</i> of the <i>width</i> and <i>precision</i>
fields of <i>std-format-spec</i>. <i>arg-id</i> now requires a signed or unsigned integer type instead 
of an integral type.</ins>
<p/>
<ins><b>Rationale:</b> Avoid types that are not useful and the need to specify enabled
  formatter specializations for all character types.</ins>
<p/>
<ins><b>Effect on original feature:</b> Valid C++ 2020 code that passes a boolean or character type 
as <i>arg-id</i> becomes invalid. For example:</ins>
</p>
<blockquote><pre>
<ins>std::format("{:*^{}}", "", true); <i>// ill-formed, previously returned "*"</i></ins>
</pre></blockquote>
</blockquote>

</li>

</ol>
</blockquote>

<p><i>[2022-11-01; Jonathan provides improved wording]</i></p>


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

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

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

<blockquote>
<p>
-8- If <code>{ <i>arg-id<sub>opt</sub></i> }</code> is used in a <i>width</i> or <i>precision</i>,
the value of the corresponding formatting argument is used in its place. If the corresponding
formatting argument is not of
<del>integral</del><ins>standard signed or unsigned integer</ins> type,
or its value is negative, an exception of type <code>format_error</code> is thrown.
</p>
</blockquote>
</li>

<li><p>Add a new paragraph to C.2.10 <a href="https://wg21.link/diff.cpp20.utilities">[diff.cpp20.utilities]</a> as indicated:</p>

<blockquote>
<p>
<ins><b>Affected subclause:</b> 28.5.2.2 <a href="https://wg21.link/format.string.std">[format.string.std]</a></ins>
<p/>
<ins><b>Change:</b> Restrict types of formatting arguments used as
<i>width</i> or <i>precision</i> in a <i>std-format-spec</i>.</ins>
<p/>
<ins><b>Rationale:</b> Avoid types that are not useful or do not have
portable semantics.</ins>
<p/>
<ins><b>Effect on original feature:</b> Valid C++ 2020 code that passes a boolean or character type
as <i>arg-id</i> becomes invalid. For example:</ins>
</p>
<blockquote><pre>
<ins>std::format("{:*^{}}", "", true); <i>// ill-formed, previously returned "*"</i></ins>
<ins>std::format("{:*^{}}", "", '1'); <i>// ill-formed, previously returned an implementation-defined number of '*' characters</i></ins>
</pre></blockquote>
</blockquote>
</li>
</ol>

</blockquote>

<p><i>[2022-11-10; Jonathan revises wording]</i></p>

<p>Improve Annex C entry.</p>
<p><i>[Kona 2022-11-10; Move to Ready]</i></p>


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



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

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

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

<blockquote>
<p>
-8- If <code>{ <i>arg-id<sub>opt</sub></i> }</code> is used in a <i>width</i> or <i>precision</i>,
the value of the corresponding formatting argument is used in its place. If the corresponding
formatting argument is not of
<del>integral</del><ins>standard signed or unsigned integer</ins> type,
or its value is negative, an exception of type <code>format_error</code> is thrown.
</p>
</blockquote>
</li>

<li><p>Add a new paragraph to C.2.10 <a href="https://wg21.link/diff.cpp20.utilities">[diff.cpp20.utilities]</a> as indicated:</p>

<blockquote>
<p>
<ins><b>Affected subclause:</b> 28.5.2.2 <a href="https://wg21.link/format.string.std">[format.string.std]</a></ins>
<p/>
<ins><b>Change:</b> Restrict types of formatting arguments used as
<i>width</i> or <i>precision</i> in a <i>std-format-spec</i>.</ins>
<p/>
<ins><b>Rationale:</b> Disallow types that do not have useful or portable
semantics as a formatting width or precision. </ins>
<p/>
<ins><b>Effect on original feature:</b> Valid C++ 2020 code that passes a boolean or character type
as <i>arg-id</i> becomes invalid. For example:</ins>
</p>
<blockquote><pre>
<ins>std::format("{:*^{}}", "", true); <i>// ill-formed, previously returned "*"</i></ins>
<ins>std::format("{:*^{}}", "", '1'); <i>// ill-formed, previously returned an implementation-defined number of '*' characters</i></ins>
</pre></blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
