Title
Cannot format const-iterable only ranges
Status
new
Section
[format.range]
Submitter
Hewill Kang

Created on 2025-03-06.00:00:00 last changed 1 month ago

Messages

Date: 2025-03-06.00:00:00

The standard does not explicitly prohibit ranges that are only const-iterable, i.e. a range with const begin() and deleted or invalid non-const begin().

Unfortunately, those ranges cannot be formatted because the R in formatter<R> is always without the const-qualifier, which makes it never satisfy the range concept (demo):

#include <print>
#include <ranges>

struct R {
  int* begin() = delete;
  int* end() = delete;
  const int* begin() const;
  const int* end() const;
};

int main() {
  const R r;
  static_assert(std::ranges::contiguous_range<decltype(r)>);

  for (auto&& elem : r)
    std::print("{} ", elem); // ok

  std::ranges::for_each(
    r, [](auto&& elem) { std::print("{} ", elem); }
  );                         // ok

  std::print("{}", r);       // not ok
}

Although such type might be relatively rare, it does reflect an inconsistency in the general usage of formatting ranges, which do not support all valid ranges.

History
Date User Action Args
2025-03-06 00:00:00admincreate