Namespaces
Variants
Views
Actions

Talk:cpp/utility/format/formatter

From cppreference.com
< Talk:cpp‎ | utility‎ | format

[edit] Reordering of arguments in output?

The referred python format allows re-ordering of the arguments in the output string. The examples on this page does not mention this feature at all. Does C++20 std::format() allows reordering or not? --2003:E3:B71E:EF00:FD3E:72FF:AAAE:2B48 02:29, 1 June 2022 (PDT)

It does, see arg-id in the format string specification in format() et al --Ybab321 (talk) 02:54, 1 June 2022 (PDT)

[edit] constexpr required for formatter<T, Char>::parse()?

While trying to implement a formatter, I noticed that both MSVC and GCC require making formatter::parse() constexpr (when used within std::format); otherwise the code doesn't compile (code on Coliru). MSVC STL (example) and GCC libc++ (example) also make their formatter::parse()s constexpr. So I think there is some sort of requirement about this; but I can't find evidence, and I don't really know much about this. Studyingegret (talk) 04:53, 8 November 2023 (PST)

I don't believe constexpr parse is a requirement of a formatter. The issue is in the above code is that std::format expects a constant expression (via the consteval of std::format_string's ctor) of the parse of the given format string. If you instead use std::vformat, then the code works out. Probably worth adding a note on BasicFormatter --Ybab321 (talk) 07:03, 8 November 2023 (PST)
I added a notice at std::format, but I'm not confident with what I wrote... --Studyingegret (talk) 07:04, 12 November 2023 (PST)

[edit] About splitting into 2 examples

I added my example and made the two examples separate. I think the style difference from other pages doesn't matter much, but feel free to revert my edit. I have left an unsplit revision in the page's edit history. Studyingegret (talk) 08:07, 8 November 2023 (PST)

overall cppreference strongly prefers having one example per page (though come to think if it, neither MOS nor FAQ say so explicitly). I'd also try to find something that needs fewer lines of code than that String to convey the same idea. Maybe some math type? --Cubbi (talk) 09:14, 8 November 2023 (PST)
Line count should be decently low now --Ybab321 (talk) 12:10, 8 November 2023 (PST)