[libcxx-commits] [libcxx] Formatting Ranges: Range "m" specifier do not pass through to underlying element (PR #70616)
Yihe Li via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Oct 29 21:21:00 PDT 2023
https://github.com/Mick235711 created https://github.com/llvm/llvm-project/pull/70616
In libc++ implementation of C++23 ranges formatting facility, the following code (MVP):
```cpp
#include <format>
#include <vector>
#include <iostream>
int main()
{
std::vector<std::pair<int, int>> vs{{1, 1}, {2, 2}};
std::cout << std::format("vs = {0}\nmap = {0:m}\nmapmap = {0:m:m}\n", vs);
return 0;
}
```
outputs ([Compiler Explorer @ x86-64 clang trunk with libc++](https://godbolt.org/z/GPboxKKoY))
```
vs = [(1, 1), (2, 2)]
map = {(1, 1), (2, 2)}
mapmap = {1: 1, 2: 2}
```
However, the m specifier for ranges should also imply m formatting (i.e. "key: value" for pair and 2-tuple) on the elements of the ranges, as per [\[tab:formatter.range.type\]/1](https://eel.is/c++draft/tab:formatter.range.type):
```
Indicates that the opening bracket should be "{", the closing bracket should be "}", the separator should be ", ",
and each range element should be formatted as if m were specified for its tuple-type.
```
In other words, `{:m}` should be equivalent to `{:m:m}`, yet in the above output it clearly isn't.
>From d590a6533652e50669accb2260b70ab25788270b Mon Sep 17 00:00:00 2001
From: Yihe Li <winmikedows at hotmail.com>
Date: Mon, 30 Oct 2023 12:19:51 +0800
Subject: [PATCH] Fix m specifier for range formatting: call
set_bracket/separator on underlying too
---
libcxx/include/__format/range_formatter.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/include/__format/range_formatter.h b/libcxx/include/__format/range_formatter.h
index d13278009fcf897..25d0f5697c49c7f 100644
--- a/libcxx/include/__format/range_formatter.h
+++ b/libcxx/include/__format/range_formatter.h
@@ -216,6 +216,8 @@ struct _LIBCPP_TEMPLATE_VIS range_formatter {
if constexpr (__fmt_pair_like<_Tp>) {
set_brackets(_LIBCPP_STATICALLY_WIDEN(_CharT, "{"), _LIBCPP_STATICALLY_WIDEN(_CharT, "}"));
set_separator(_LIBCPP_STATICALLY_WIDEN(_CharT, ", "));
+ __underlying_.set_brackets({}, {});
+ __underlying_.set_separator(_LIBCPP_STATICALLY_WIDEN(_CharT, ": "));
++__begin;
} else
std::__throw_format_error("Type m requires a pair or a tuple with two elements");
More information about the libcxx-commits
mailing list