Skip to content

Commit f843be7

Browse files
committed
New issue from Hewill: "Cannot format const-iterable only ranges"
1 parent 4c5cbff commit f843be7

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

xml/issue4221.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version='1.0' encoding='utf-8' standalone='no'?>
2+
<!DOCTYPE issue SYSTEM "lwg-issue.dtd">
3+
4+
<issue num="4221" status="New">
5+
<title>Cannot format const-iterable only ranges</title>
6+
<section>
7+
<sref ref="[format.range]"/>
8+
</section>
9+
<submitter>Hewill Kang</submitter>
10+
<date>06 Mar 2025</date>
11+
<priority>99</priority>
12+
13+
<discussion>
14+
<p>
15+
The standard does not explicitly prohibit ranges that are only const-iterable, i.e. a range with
16+
<code>const begin()</code> and deleted or invalid non-<code>const begin()</code>.
17+
</p>
18+
<p>
19+
Unfortunately, those ranges cannot be formatted because the <code>R</code> in
20+
<code>formatter&lt;R&gt;</code> is always without the <code>const</code>-qualifier,
21+
which makes it never satisfy the <code>range</code> concept
22+
(<a href="https://godbolt.org/z/68T6EjKG4">demo</a>):
23+
</p>
24+
<blockquote><pre>
25+
#include &lt;print&gt;
26+
#include &lt;ranges&gt;
27+
28+
struct R {
29+
int* begin() = delete;
30+
int* end() = delete;
31+
const int* begin() const;
32+
const int* end() const;
33+
};
34+
35+
int main() {
36+
const R r;
37+
static_assert(std::ranges::contiguous_range&lt;decltype(r)&gt;);
38+
39+
for (auto&amp;&amp; elem : r)
40+
std::print("{} ", elem); // ok
41+
42+
std::ranges::for_each(
43+
r, [](auto&amp;&amp; elem) { std::print("{} ", elem); }
44+
); // ok
45+
46+
std::print("{}", r); // <span style="color:red;font-weight:bolder">not ok</span>
47+
}
48+
</pre></blockquote>
49+
<p>
50+
Although such type might be relatively rare, it does reflect an inconsistency in the general usage of formatting
51+
ranges, which do not support all valid ranges.
52+
</p>
53+
</discussion>
54+
55+
<resolution>
56+
</resolution>
57+
58+
</issue>

0 commit comments

Comments
 (0)