Search⌘ K
AI Features

User-defined Types

Explore how to format user-defined types using the C++20 Formatting Library. Understand specializing std::formatter by implementing parse and format methods to customize the display of containers like std::vector. Learn to apply format specifications, handle line breaks, and output neatly aligned, readable data.

We'll cover the following...

To format a user-defined type, I have to specialize the class std::formatter for my user-defined type. This means, in particular, I have to implement the member functions parse and format.

  • parse:

    • Accepts the parse context
    • Parses the parse context
    • Returns an iterator to the end of the format specification
    • Throws a std::format_error in case of an error
  • format:

    • Gets the value t, which should be formatted, and the format context fc
    • Formats t according to the format context
    • Writes the output to fc.out()
    • Returns
...