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 14 '26
You wouldn't. That's the whole point. In this situation you are required to explicitly construct a new buffer with a terminating zero.
Consider your options when you want to call a C-function that needs a terminating zero from some lower-level function in your own code. For your own interface, you currently have three options:
All three of these have disadvantages:
How are you going to call these functions? In my experience: calling them with a string literal is extremely common (and string literals have terminating zeroes). Calling them with std::string is also extremely common (and again, they have terminating zeroes). And once in a while you find yourself dealing with something that cuts a substring out of a string_view, but that's comparatively rare. Now look at what cstring_view offers:
And that's why we want cstring_view.