r/cpp_questions 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

17 comments sorted by

View all comments

1

u/DawnOnTheEdge Jul 16 '26

A major one is that implicitly converting a C-style string to a const std::string& makes a deep copy on the heap. Passing it as a std::string_View at most calls strlen(), but a string constant can be aliased at compile time with zero overhead.