3 ms·
std::string_view with %.*s as a string formatter work well enough if you're set on using printf.
by humanrebar 2y ago
std::string_view with %.*s as a string formatter work well enough if you're set on using printf.
- gpderetta 2y agoI have to use std::string_view with %.*s as a string formatter every day. It is an incredible pain. Of course the solution is getting rid of printf style formatting, not string_view.
- o11c 2y agoThe hardest part of passing `std::string` to `printf` was getting the macro to convince the pre-standard-C++11 compiler that it was `constexpr` enough so that `-Werror=format` would work. I found it was easiest to sometimes allocate a temporary, if the input wasn't known to be NUL-terminated.
- jeffbee 2y agoAre you suggesting printf("%.*s", static_cast<int>(sv.size()), sv.data()) ?
- humanrebar 2y agoWell, wrap it up in something more ergonomic, but it works. I'm usually putting strings into some sort of I/O API (like a logger) instead of hardcoding copies to stdout everywhere. https://stackoverflow.com/a/72575650 https://stackoverflow.com/a/72575650
- jeffbee 2y agoWrap it way up into something ergonomic and idiomatic, like fmt.
- humanrebar 2y agoSure. I do. But there are tradeoffs even then. Look at the error messages compared to printf for example.