7 comments

[ 3.1 ms ] story [ 26.9 ms ] thread
cheap jabs at the venerable printf(...) notwithstanding, this article is ok. however, i must confess, string formatting still sucks in plain C++, and snprintf(...) is not too shabby.
The cheap jabs are all incorrect, as well. Any compiler from the last 10 years will warn you if you don't match your printf format string to the arguments. printf also supports reordering arguments using the %m$ syntax (read the man page). And yes, you can extend printf with your own types.

A better guide to using strings in C++ is here: https://yosefk.com/c++fqa/io.html

Does the compiler also warn when you don't use a literal format string?
I’m surprised at how the author fails to mention fmtlib [1] with near-printf-like performance and sane (Python .format-like) formatting syntax, which also happens to generate much smaller binaries than Boost.Format.

[1] https://github.com/fmtlib/fmt

Author doesn't have a comment section unfortunately, perhaps he reads this. Instead of the stream iterator, use a stream joiner and not deal with the crummy trailing delimiter.

Also to_chars offers better formatting than to_string.

to late to edit: there IS a comment section but my ad blocking had blocked it -- and someone else pointed out the stream joiners.
Informative article. In practice, using ostringstream is the most useful technique.