Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

  • There's fmt::format(...) -> std::string which makes sense, it returns a formatted string.
  • Then we have void fmt::print([stream, ] ...) which also makes sense naming wise (certainly given the printf legacy).
  • But then we have fmt::format_to(OutputIt, ...) -> OutputIt which resembles, apart from the return type, what print does with streams.

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 fmt tag here, I was hoping that we could get a authoritative answer on this from the original API authors.

like image 210
Martin Ba Avatar asked Jan 24 '23 07:01

Martin Ba


1 Answers

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.

like image 182
vitaut Avatar answered Jan 26 '23 22:01

vitaut