|
| 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<R></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 <print> |
| 26 | +#include <ranges> |
| 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<decltype(r)>); |
| 38 | + |
| 39 | + for (auto&& elem : r) |
| 40 | + std::print("{} ", elem); // ok |
| 41 | + |
| 42 | + std::ranges::for_each( |
| 43 | + r, [](auto&& 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