News for hackers. Stuff that matters.
Re:C++ Anti-Patterns by jhalstead on Friday January 22, 2021 @01:18AM (#25868949)
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

Re:C++ Anti-Patterns by OskarS on Friday January 22, 2021 @05:39AM (#25870413)
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.

Re:C++ Anti-Patterns by lldbg on Saturday January 23, 2021 @05:02AM (#25881120)
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.
Re:C++ Anti-Patterns by cozzyd on Friday January 22, 2021 @02:41AM (#25869393)
That looks pretty good! Too bad it's not part of the standard library.
Re:C++ Anti-Patterns by jcelerier on Friday January 22, 2021 @09:41AM (#25871995)