r/cpp • u/pavel_v • Apr 30 '26
C++26: string and string_view improvements
https://www.sandordargo.com/blog/2026/04/29/cpp26-string-string_view-improvements
70
Upvotes
r/cpp • u/pavel_v • Apr 30 '26
View all comments
Show parent comment
2
u/johannes1971 May 13 '26
How does string_view guarantee that the buffer is valid? How does span guarantee the buffer is valid? How does any view guarantee that?
cstring_view, when constructed from either a string literal or a std::string, is guaranteed to have a zero terminator. You can add some corner cases (like substrings of the aforementioned that run until the end) but really, this is already enough.
And you absolutely cannot do the same with string_view. Sure, you can say "well I only ever call it with zero terminators so I know it's safe", but it only takes one mistake to have a buffer overrun. And why would you ever chose that, given that a type-safe solution is easily possible and would guarantee that no mistakes get made?