I am new to C++17 and to std::string_view
.
I learned that they are not null terminated and must be handled with care.
Is this the right way to printf() one?
#include<string_view>
#include<cstdio>
int main()
{
std::string_view sv{"Hallo!"};
printf("=%*s=\n", static_cast<int>(sv.length()), sv.data());
return 0;
}
(or use it with any other printf-style function?)
printf
in c++ at all?string_view
can be non-null terminated, but only if you create it as such."Hallo"
has a null terminator, sosv
will as well."=%.*s=\n"
... Just checked, that's correct.fmt
library it should easy to transform code which usesprintf
.