The Surprising Complexity of Formatting Ranges in Cpp - Barry Revzin - CppCon 2022

CppCon · 4K views · 2023 · 57:51

Open on YouTube

C, C++, Rust, Go & systems languagesProgramming language designtalkdated

Why Stack Overflow recommends it

Stack Overflow users link it when asking about or explaining how std::format and std::formatter work for ranges, and what is planned for C++23.

Recommended by 0 people across 0 different months, 2023 to 2023.

What it is

A CppCon 2022 talk by Barry Revzin on adding range and tuple formatting to std::format for C++23. He starts with how the std::format API differs from ostreams, then covers the technical hurdles of formatting ranges and especially tuples, and the facility being standardized.

What people said

Cites Barry · 2023-02 · Stack Overflow · on “How to forward formatting information from std::format”

This is a bad goal: std::format("{:x}{:*^10}", p) This is an expression that looks like it's formatting two arguments, but is actually formatting only one. Sure, you can make it work, but... this is incredibly confusing. A better goal would instead to make this work: std::format("{:{x}{*^10}}", p) That is, you only have one replacement-field, whose format-spec is {x}{*^10} . This is more consistent with the way the rest of the library works. And then we can do all the things we need to do more directly, using formatter<int> and formatter<string> : template <> struct std::formatter<Person> { std::formatter<int> age; std::formatter<std::string> name; template<typename ParseContext> constexpr a

Cites W Murphy · 2023-04 · Stack Overflow · on “status of C++23 formatting ranges”

I watched Barry Revzin's talk on Formatting Ranges [this video] that was very interesting. I see the feature status page [LINK] that Formatting Ranges is still planned for C++23, ad it says clang has implemented it. But when I try some of Barry's examples on godbolt [LINK] (with -std=c++2b), I get compiler errors, and today getting core dumps. Plus when Googling around, I don't see many talking about it. I don't even see much on cppreference [LINK] ? Understandably the release isn't complete, but seems odd that documentation is lacking, and nobody is taking about it? not much chatter about it Godbolt isn't compiling not much documentation on CppReference

Cites Red.Wave · 2023-11 · Stack Overflow · on “How do I use std::formatter directly?”

I don't understand why you need to touch the context. Context is obviously supposed to be reserved for library internals, not for user to tamper with. It os created from the upperhand range which maybe created from a stream. You need to create a costume range class and let `format_to` create the context. If you want to create a temporary context for a range type, then good luck with that: [this video]