fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
19.84k stars 2.42k forks source link

format_as but with iterator #3960

Closed cruisercoder closed 1 month ago

cruisercoder commented 1 month ago

Could we get something like format_as but that takes a context/iterator so that it is closer to the specialization extension method?

namespace geo {
  struct Point {
    int x, y;
  };

  auto formatter(fmt::format_context &ctx, const Point &p) {
    return format_with<std::string>(ctx.out(), "({},{})", p.x, p.y);
  }
}

I realized that format_as, while useful, is not going to be as efficient when internal formatting needs to be done for a type with several members. The naming may be problematic as it may conflict with names in the fmt space (maybe format_to). I was thinking that format_as might have paved the way for multiple custom type extension points, but I'm guessing there is a reason why this is problematic.

This would make it easier to format a large number of types that don't need custom format specifications and keeps the definition close to the type. There is probably a better mechanism than format_with to indicate which spring specifiers to inherit from (although none might be an option)

vitaut commented 1 month ago

Thanks for the suggestion but we should keep the number of extension points to a minimum. You could probably achieve what you want with a generic formatter without any changes to {fmt}.