Martin Ba
Martin Ba

Reputation: 38891

Technical background to the C++ fmt::print vs. fmt::format_to naming?

Why is it fmt::format_to(OutputIt, ...) and not fmt::print(OutputIt, ...)??


I'm currently familiarizing myself with {fmt}, a / the modern C++ formatting library.

While browsing the API, I found the naming a bit disjoint, but given my little-to-no experience with the library (and my interest in API design), I would like to get behind these naming choices: (fmt core API reference)

Now obviously, one can bike shed names all day, but here the question is not on why we have format vs. print (which is quite explainable to me), but why a function that clearly(?) behaves like the write-to-stream-kind has been bundled with the format_... naming style.

So, as the question title already asks, is there a technical difference in how fmt::print(stream, ...) behaves when formatting to a streams vs. how fmt::format_to(OutputIt, ...) behaves when formatting to an output iterator?

Or was/is this purely a style choice? Also, given that the GitHube repo explicitly lists the tag here, I was hoping that we could get a authoritative answer on this from the original API authors.

Upvotes: 4

Views: 2290

Answers (1)

vitaut
vitaut

Reputation: 55605

While it would be possible to name format_to print, the former is closer conceptually to format than to print. format_to is a generalization of format that writes output via an output iterator instead of std::string. Therefore the naming reflects that.

print without a stream argument on the other hand writes to stdout and print with an argument generalizes that to an arbitrary stream. Writing to a stream is fundamentally different from writing to an output iterator because it involves additional buffering, synchronization, etc. Other languages normally use "print" for this sort of functionality so this convention is followed in {fmt}.

This becomes blurry because you can have an output iterator that writes to a stream but even there the current naming reflects what high-level APIs are being used.

In other words format_to is basically a fancy STL algorithm (there is even format_to_n similarly to copy/copy_n) while print is a formatted output function.

Upvotes: 10

Related Questions