Skip to content

Print a type for debugging

=rbe::fmt gives a struct a std::formatter and an operator<< that print every member, recursively, with the member names.

Source: example/howto/format.cpp, CMake target howto_format.

// rbe::fmt opts a type into the formatter. A nested struct needs it too, or a std::formatter of its own.
struct [[=rbe::fmt]] Price {
  std::int64_t mantissa;
  std::uint8_t exponent;
};

// rbe::debug is the preset for rbe::fmt, and composes with the wire annotations
struct [[=rbe::pack_be, =rbe::debug]] Order {
  std::uint32_t order_id;
  std::uint32_t quantity;
  Price         price;
};
Order const order {.order_id = 7, .quantity = 100, .price = {.mantissa = 2500, .exponent = 2}};

std::println("{}", order);                         // std::print and std::println
std::string text = std::format("{}", order);       // std::format
std::cout << order << '\n';                        // and streams

// Order {
//   .order_id = 7,
//   .quantity = 100,
//   .price = Price {
//     .mantissa = 2500,
//     .exponent = 2,
//   },
// }
  • A nested struct is printed through its own formatter, so it needs =rbe::fmt too (or a std::formatter of its own). Members of standard types print as std::format prints them.
  • rbe::debug is rbe::derive<rbe::fmt>: a preset, so it reads like the wire presets next to it.
  • The annotation has no effect on the wire.

See also: Core reference, formatting.