Skip to content

Make ranges constexpr formattable - #4979

Open
ilazaric wants to merge 2 commits into
fmtlib:mainfrom
ilazaric:ilazaric/rg
Open

ilazaric wants to merge 2 commits into
fmtlib:mainfrom
ilazaric:ilazaric/rg

Conversation

@ilazaric

@ilazaric ilazaric commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Added ranges-formatting FMT_COMPILE tests were not working previously. This is because of a discrepancy between compile.h and format.h, former was using std::back_inserter(std::string), while latter was using back_appender(basic_memory_buffer).

This is actually needed because a different part has a bug:

  template <typename FormatContext, typename F, typename... T>
  FMT_CONSTEXPR auto write(FormatContext& ctx, const F& f, T&&... values) const
      -> decltype(ctx.out()) {
    auto specs = specs_;
    handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, ctx);
    if (specs.width == 0) return f.write_body(ctx, static_cast<T&&>(values)...);

    auto buf = basic_memory_buffer<Char>();
    auto buffer_ctx =
        FormatContext(basic_appender<Char>(buf), ctx.args(), ctx.locale());
    f.write_body(buffer_ctx, static_cast<T&&>(values)...);
    return detail::write<Char>(
        ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs);
  }

The snippet tries to construct a FormatContext using a different iterator type.
It didn't seem obvious how to fix it, as ctx.args() is also context specific.

This PR is a cleanup of #4956

Added ranges-formatting FMT_COMPILE tests were not working previously.
This is because of a discrepancy between compile.h and format.h,
former was using `std::back_inserter(std::string)`,
while latter was using `back_appender(basic_memory_buffer)`.
@ilazaric
ilazaric requested a review from vitaut as a code owner October 9, 2026 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant