Hacker News new | ask | show | jobs
by jhalstead 1967 days ago
If you're interested, check out Abseil's StrFormat [0] for a typesafe printf replacement that uses format strings. You get a nice speed up as well.

[0] https://abseil.io/docs/cpp/guides/format

2 comments

It's blows my mind that something like this hasn't been in the standard library for, like, 20 years. Like, why can't i do this?

   std::string str = "Values: ";

   for (auto &[key, val]: dict) {
       str.append("%s: %d, ", key, value);
   }
This is so much nicer to use than stringstream, and it's also so much more efficient! You could allocate a "big enough" string at the beginning, and then you would have no more allocations. You could also reuse the string, so you can avoid almost all allocations for the lifetime of the program. This is basically impossible to do with stringstream: it's just insane about copies and allocations, and it's more or less impossible to reuse buffers.

The C++ format strings are an improvement, but just this simple thing (a super straight-forward translation of printf to C++ std::strings) is basically all you would ever need. C more or less got it right in the 70s, and then C++ spent decades with its own (significantly worse) solution.

The original reason is that it was not possibly to implement in a type-safe manner with c++98, as it lacked variadic templates. The << stream << stuff was always a compromise.
That looks pretty good! Too bad it's not part of the standard library.