4 ms·
strong disagree on printf... maybe there's some modern typesafe C++ way to do the same thing, but format specifiers are way easier to remember than all the ways
by cozzyd 6y ago
strong disagree on printf... maybe there's some modern typesafe C++ way to do the same thing, but format specifiers are way easier to remember than all the ways of modifying iostream.
- tcbawo 6y agoagreed. The printf style API is great for deferred/asynchronous log formatting via serialization.
- DerekL 6y agoThere's a new formatting library in C++20. Unlike printf, it's type-safe, and it's extensible to new types.
- jhalstead 6y agoIf 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 https://abseil.io/docs/cpp/guides/format
- cozzyd 6y agoThat looks pretty good! Too bad it's not part of the standard library.
- jcelerier 6y agohttps://en.cppreference.com/w/cpp/utility/format/format https://en.cppreference.com/w/cpp/utility/format/format
- OskarS 6y agoIt'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.
- lldbg 6y agoThe 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.
- captainmuon 6y agoAnd at least on clang, printf is somewhat typesafe. If you write %s and pass an int, it will give you a warning. This also works if you are using a wrapper that ultimatively calls printf! Constrast with cout, which will happily let you print out the wrong variable. It won't print out garbage of course because it helpfully knows to print out the int differently, but it can't give you a warning. Also, it's difficult to do localization with it.
- zorgmonkey 6y agoC++20 adds std::format [0] which is based on the excellent fmtlib [1] (both type safe and claims to be substantially faster than printf) [0] https://en.cppreference.com/w/cpp/utility/format/format https://en.cppreference.com/w/cpp/utility/format/format [1] https://fmt.dev https://fmt.dev
- carlmr 6y agoC++20 is the distant future for a lot of code bases.