Skip to content

Conversation

Mick235711
Copy link
Contributor

In libc++ implementation of C++23 ranges formatting facility, the following code (MVP):

#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++)

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:

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.

@Mick235711 Mick235711 requested a review from a team as a code owner October 30, 2023 04:20
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Oct 30, 2023
@github-actions
Copy link

github-actions bot commented Oct 30, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@Mick235711
Copy link
Contributor Author

Hmm, not really sure why the test fails here but pass in c++23. Is there anything changed on range formatting in c++2c?

@philnik777 philnik777 added the format C++20 std::format or std::print, and anything related to them label Oct 30, 2023
@ldionne ldionne requested a review from mordante October 31, 2023 03:37
@mordante
Copy link
Member

Thanks for the patch!
As mentioned here #60995 (comment) I wonder whether it is intended that :m overrides n of the underlying tuple-type formatter. I want to discuss this with the Committee before looking at the details of this patch.

@Mick235711
Copy link
Contributor Author

Well, that's an unlikely title for the same issue...😄
Sure, I guess this should be an LWG issue then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
format C++20 std::format or std::print, and anything related to them libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants