by RcouF1uZ4gsC · 2y ago · 0 comments
Having a string abstraction beyond C null-terminated string is a huge deal both for convenience and for safety. It is super easy to get strings wrong in C.
This is also one of the reasons why printf won't work. You cannot pass a type like std::string to a variable args function.
Too often I have to fix code that makes liberal use of std::string, making copies and allocations everywhere for no apparent reason, when string_view or better const char* would do.
Can't you just call .c_str() on the std::string first to get to the null-terminated char array? https://cplusplus.com/reference/string/string/c_str/
I found it was easiest to sometimes allocate a temporary, if the input wasn't known to be NUL-terminated.
printf("%.*s", static_cast<int>(sv.size()), sv.data())
? std::string str;
printf("%s\n", str.c_str());