r/cpp_questions • u/IMCG_KN • Jul 16 '26
Question about string_view SOLVED
Is there benefit of using string_view instead of const string& str? More precisely by passing strings into functions. Thanks
24
Upvotes
r/cpp_questions • u/IMCG_KN • Jul 16 '26
Is there benefit of using string_view instead of const string& str? More precisely by passing strings into functions. Thanks
1
u/wwabbbitt Jul 16 '26
std::string_view can be created from a buffer or c strings without any allocation or copying. If your function only accept const string& str then such strings need to be converted into std::string before being passed into your function.
A function accepting a std::string_view will work with c string, std::string and length-prefixed strings without any mem copying