News for hackers. Stuff that matters.
Re:Orthodox C++ by humanrebar on Wednesday May 22, 2024 @06:18PM (#40447396)
std::string_view with %.*s as a string formatter work well enough if you're set on using printf.
Re:Orthodox C++ by gpderetta on Wednesday May 22, 2024 @06:21PM (#40447419)
I 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.
Re:Orthodox C++ by o11c on Wednesday May 22, 2024 @06:45PM (#40447692)
The 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.

Re:Orthodox C++ by jeffbee on Wednesday May 22, 2024 @06:25PM (#40447462)
Are you suggesting

  printf("%.*s", static_cast<int>(sv.size()), sv.data())

?
Re:Orthodox C++ by humanrebar on Wednesday May 22, 2024 @06:34PM (#40447566)
Well, 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

Re:Orthodox C++ by jeffbee on Wednesday May 22, 2024 @06:54PM (#40447779)
Wrap it way up into something ergonomic and idiomatic, like fmt.
Re:Orthodox C++ by humanrebar on Wednesday May 22, 2024 @08:12PM (#40448661)
Sure. I do. But there are tradeoffs even then. Look at the error messages compared to printf for example.