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
4
u/johannes1971 May 07 '26
It would work by only being constructible from a string that already has a zero terminator, obviously. You are correct that string_view does not make this guarantee, but that's why this is a different type. You could construct it from string literals and from std::string, two extremely common use cases.
I'd be very happy with it: right now I'm using string_view for function arguments basically everywhere, but this means that quite often at the 'bottom' of the stack I need to allocate a buffer because I need a terminating zero. With this type you can ask for a cstring_view (or zstring_view as it's also sometimes called) from the bottom up, and only convert from string_view in code that happens to produce a string_view.