I have a method that takes std::string_view
and uses function, which takes null terminated string as parameter. For example:
void stringFunc(std
You cannot alter a string through std::string_view
. Therefore you cannot add a terminating '\0'
character. Hence you need to copy the string somewhere else to add a '\0'
-terminator. You could avoid heap allocations by putting the string on the stack, if it's short enough. If you know, that the std::string_view
is part of a null-terminated string, then you may check, if the character past the end is a '\0'
character and avoid the copy in that case. Other than that, I don't see much more room for optimizations.