|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4240: The formattable type is not a formattable type</title> |
| 6 | +<meta property="og:title" content="Issue 4240: The formattable type is not a formattable type"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4240.html"> |
| 9 | +<meta property="og:type" content="website"> |
| 10 | +<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png"> |
| 11 | +<meta property="og:image:alt" content="C++ logo"> |
| 12 | +<style> |
| 13 | + p {text-align:justify} |
| 14 | + li {text-align:justify} |
| 15 | + pre code.backtick::before { content: "`" } |
| 16 | + pre code.backtick::after { content: "`" } |
| 17 | + blockquote.note |
| 18 | + { |
| 19 | + background-color:#E0E0E0; |
| 20 | + padding-left: 15px; |
| 21 | + padding-right: 15px; |
| 22 | + padding-top: 1px; |
| 23 | + padding-bottom: 1px; |
| 24 | + } |
| 25 | + ins {background-color:#A0FFA0} |
| 26 | + del {background-color:#FFA0A0} |
| 27 | + table.issues-index { border: 1px solid; border-collapse: collapse; } |
| 28 | + table.issues-index th { text-align: center; padding: 4px; border: 1px solid; } |
| 29 | + table.issues-index td { padding: 4px; border: 1px solid; } |
| 30 | + table.issues-index td:nth-child(1) { text-align: right; } |
| 31 | + table.issues-index td:nth-child(2) { text-align: left; } |
| 32 | + table.issues-index td:nth-child(3) { text-align: left; } |
| 33 | + table.issues-index td:nth-child(4) { text-align: left; } |
| 34 | + table.issues-index td:nth-child(5) { text-align: center; } |
| 35 | + table.issues-index td:nth-child(6) { text-align: center; } |
| 36 | + table.issues-index td:nth-child(7) { text-align: left; } |
| 37 | + table.issues-index td:nth-child(5) span.no-pr { color: red; } |
| 38 | + @media (prefers-color-scheme: dark) { |
| 39 | + html { |
| 40 | + color: #ddd; |
| 41 | + background-color: black; |
| 42 | + } |
| 43 | + ins { |
| 44 | + background-color: #225522 |
| 45 | + } |
| 46 | + del { |
| 47 | + background-color: #662222 |
| 48 | + } |
| 49 | + a { |
| 50 | + color: #6af |
| 51 | + } |
| 52 | + a:visited { |
| 53 | + color: #6af |
| 54 | + } |
| 55 | + blockquote.note |
| 56 | + { |
| 57 | + background-color: rgba(255, 255, 255, .10) |
| 58 | + } |
| 59 | + } |
| 60 | +</style> |
| 61 | +</head> |
| 62 | +<body> |
| 63 | +<hr> |
| 64 | +<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> |
| 65 | +<h3 id="4240"><a href="lwg-active.html#4240">4240</a>. The formattable type is not a <code>formattable</code> type</h3> |
| 66 | +<p><b>Section:</b> 28.5.6.3 <a href="https://wg21.link/format.formattable">[format.formattable]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2025-04-06 <b>Last modified:</b> 2025-04-06</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View other</b> <a href="lwg-index-open.html#format.formattable">active issues</a> in [format.formattable].</p> |
| 71 | +<p><b>View all other</b> <a href="lwg-index.html#format.formattable">issues</a> in [format.formattable].</p> |
| 72 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 73 | +<p><b>Discussion:</b></p> |
| 74 | +<p> |
| 75 | +User-specific <code>formatter</code>s usually have the following form: |
| 76 | +</p> |
| 77 | +<blockquote><pre> |
| 78 | +template <> struct std::formatter<T> { |
| 79 | + constexpr auto parse(format_parse_context& ctx) |
| 80 | + -> format_parse_context::iterator; |
| 81 | + |
| 82 | + auto format(const T& value, format_context& ctx) const |
| 83 | + -> format_context::iterator; |
| 84 | +}; |
| 85 | +</pre></blockquote> |
| 86 | +<p> |
| 87 | +This is reflected in wording examples such as 28.5.6.4 <a href="https://wg21.link/format.formatter.spec">[format.formatter.spec]</a> bullet 8 or |
| 88 | +28.5.6.7 <a href="https://wg21.link/format.context">[format.context]</a> bullet 9: |
| 89 | +</p> |
| 90 | +<blockquote><pre> |
| 91 | +#include <format> |
| 92 | +#include <string> |
| 93 | + |
| 94 | +enum color { red, green, blue }; |
| 95 | +const char* color_names[] = { "red", "green", "blue" }; |
| 96 | + |
| 97 | +template<> struct std::formatter<color> : std::formatter<const char*> { |
| 98 | + auto format(color c, format_context& ctx) const { |
| 99 | + return formatter<const char*>::format(color_names[c], ctx); |
| 100 | + } |
| 101 | +}; |
| 102 | +</pre></blockquote> |
| 103 | +<p> |
| 104 | +which allows us to format <code>color</code> with <code>std::format("{}", red)</code>. |
| 105 | +Unfortunately, even so, the <code>color</code> still does not satisfy <code>std::formattable</code>. |
| 106 | +<p/> |
| 107 | +This is because the concept <code>formattable</code> is currently defined as follows: |
| 108 | +</p> |
| 109 | +<blockquote><pre> |
| 110 | +template<class T, class Context, |
| 111 | + class Formatter = typename Context::template formatter_type<remove_const_t<T>>> |
| 112 | + concept <i>formattable-with</i> = // <i>exposition only</i> |
| 113 | + semiregular<Formatter> && |
| 114 | + requires(Formatter& f, const Formatter& cf, T&& t, Context fc, |
| 115 | + basic_format_parse_context<typename Context::char_type> pc) |
| 116 | + { |
| 117 | + { f.parse(pc) } -> same_as<typename decltype(pc)::iterator>; |
| 118 | + { cf.format(t, fc) } -> same_as<typename Context::iterator>; |
| 119 | + }; |
| 120 | + |
| 121 | +template<class T, class charT> |
| 122 | + concept formattable = |
| 123 | + <i>formattable-with</i><remove_reference_t<T>, basic_format_context<<i>fmt-iter-for</i><charT>, charT>>; |
| 124 | +</pre></blockquote> |
| 125 | +<p> |
| 126 | +where <code><i>fmt-iter-for</i><charT></code> is an unspecified type that can write |
| 127 | +<code>charT</code>, which for <code>char</code> is <code>back_insert_iterator<string></code> |
| 128 | +and <code>char*</code> in libstdc++ and libc++, respectively. |
| 129 | +</p> |
| 130 | +<p> |
| 131 | +That is, for <code>color</code> to satisfy <code>formattable</code>, it is |
| 132 | +necessary to ensure that <code>cf.format(t, fc)</code> is well-formed. |
| 133 | +</p> |
| 134 | +<p> |
| 135 | +However, the <code>format()</code> function in the above example takes a <code>format_context</code> |
| 136 | +whose <code>Out</code> parameter is internal iterator type, namely |
| 137 | +<code>_Sink_iter<char></code> and <code>__format::__output_buffer<char></code> in |
| 138 | +libstdc++ and libc++, respectively. |
| 139 | +Since <code>basic_format_context</code> with different <code>Out</code> parameters cannot be converted to |
| 140 | +each other, the constraint is not satisfied. |
| 141 | +</p> |
| 142 | +<p> |
| 143 | +The reason <code>color</code> can still be formatted is that <code>basic_format_arg</code> |
| 144 | +checks for <code><i>formattable-with</i><Context></code> where <code>Context</code> |
| 145 | +has been correctly specified as <code>format_context</code>. |
| 146 | +</p> |
| 147 | +<p> |
| 148 | +And since <code>color</code> is formattable but not <code>formattable</code>, this further |
| 149 | +prevents formatting a range with elements of <code>color</code>, because the <code>formatter</code> |
| 150 | +specialization for ranges requires that the element type must be <code>formattable</code>. |
| 151 | +This leads to some inconsistencies (<a href="https://godbolt.org/z/Y8a6WTrK1">demo</a>): |
| 152 | +</p> |
| 153 | +<blockquote><pre> |
| 154 | +std::println("{}", red); // ok |
| 155 | +static_assert(std::formattable<color, char>); // <span style="color:#C80000;font-weight:bold">fires</span> |
| 156 | + |
| 157 | +std::vector<color> v; |
| 158 | +std::println("{}", v); // <span style="color:#C80000;font-weight:bold">not ok</span> |
| 159 | +</pre></blockquote> |
| 160 | +<p> |
| 161 | +The workaround is to turn the custom <code>format()</code> into a template function |
| 162 | +such as <code>format(color c, auto& ctx)</code> or |
| 163 | +<code>format(color c, basic_format_context<Out, charT>& ctx)</code>, |
| 164 | +However, this seems mandate users to always declare <code>format()</code> as the template |
| 165 | +function for the best practice, which in my opinion defeats the purpose of introducing |
| 166 | +<code>format_context</code> in the first place. |
| 167 | +</p> |
| 168 | +<p> |
| 169 | +Also, since <code><i>fmt-iter-for</i><charT></code> is unspecified, if it is specified |
| 170 | +in some library implementation as the same type as <code>format_context</code>'s <code>Out</code> |
| 171 | +parameters, then <code>color</code> will suddenly become <code>formattable</code>. This lack |
| 172 | +of guarantee about <code>formattable</code> can bring unnecessary confusion. |
| 173 | +</p> |
| 174 | +<p> |
| 175 | +I think we should ensure that <code>color</code> is <code>formattable</code>, because it is formattable. |
| 176 | +</p> |
| 177 | + |
| 178 | + |
| 179 | +<p id="res-4240"><b>Proposed resolution:</b></p> |
| 180 | +<p> |
| 181 | +This wording is relative to <a href="https://wg21.link/N5008">N5008</a>. |
| 182 | +</p> |
| 183 | + |
| 184 | +<ol> |
| 185 | + |
| 186 | +<li><p>Modify 28.5.6.3 <a href="https://wg21.link/format.formattable">[format.formattable]</a> as indicated:</p> |
| 187 | + |
| 188 | +<blockquote> |
| 189 | +<p> |
| 190 | +-1- <del>Let <code><i>fmt-iter-for</i><charT></code> be an unspecified type that models |
| 191 | +<code>output_iterator<const charT&></code> (24.3.4.10 <a href="https://wg21.link/iterator.concept.output">[iterator.concept.output]</a>)</del>. |
| 192 | +</p> |
| 193 | +<blockquote><pre> |
| 194 | +[…] |
| 195 | +template<class T, class charT> |
| 196 | + concept formattable = |
| 197 | + <i>formattable-with</i><remove_reference_t<T>, <ins>conditional_t<same_as<charT, char>, format_context, wformat_context></ins><del>basic_format_context<<i>fmt-iter-for</i><charT>, charT></del>>; |
| 198 | +</pre></blockquote> |
| 199 | +</blockquote> |
| 200 | + |
| 201 | +</li> |
| 202 | + |
| 203 | +</ol> |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | +</body> |
| 210 | +</html> |
0 commit comments