When would I choose
std::string
byconst&
instead ofstring_view
for function arguments?
Do you need a null-terminated string? If so, then you should use std::string const&
which gives you that guarantee. string_view
does not – it’s simply a range of const char
.
If you do not need a null-terminated string, and you do not need to take ownership of the data, then you should use string_view
. If you do need to take ownership of the data, then it may be the case that string
by value is better than string_view
.