
On Fri, Sep 26, 2025 at 9:23 AM Claudio DeSouza via Boost < boost@lists.boost.org> wrote:
Hey,
A type like this is extremely important, especially because `string_view` easily becomes a footgun when one doesn't realise that `data()` is not guaranteed to be null-terminated, or that the size itself is not pointing to the actual null-terminator. Additionally, a cstring view type helps with unnecessary conversions to string with the intent to guarantee that a string is null-terminated. There's also a lot to be said that using raw `const char*` is a source of problems, so you want to treat null-terminated strings as a contiguous range, similar to span, and where there are no surprises about the length, but at the same time with all the goodies that come with a string-specific type.
I did check the generated code and if one just wraps a C-API like so: void posix_foo(const char * p); void foo(cstring_view cs) {posix_foo(cs.c_str());} And then calls it with a static string foo("bar"); The optimizer removes the strlen calculation if foo can be inlined. I have only very few places in my code where a cstring_ref gets accepted by a compiled function and those few could easily be modified. That is to say: the strlen overhead doesn't exist in the generated code when inlined. So calculating the size has no downside for my use-case.
Chromium actually offers a type like this, and there are even extensions to Chromium's span type to handle conversion to byte span that should or should not include the null-terminator. For reference, link below:
https://source.chromium.org/chromium/chromium/src/+/main:base/strings/cstrin...
I would like an extended signature of this https://source.chromium.org/chromium/chromium/src/+/main:base/strings/cstrin... from `same_as<basic_string<char>>` to `string_like` : template<typename T> concept string_like = requires (const T & t) { {t.size()} -> std::convertible_to<std::size_t>; {t.data()} -> std::same_as<const T::value_type *>; {t.c_str()} -> std::same_as<const T::value_type *>; }; Which would mean that `foo(cstring_view)` can be called with a `boost::urls::url` for example. In my mind this would reflect the behaviour of std::string_view that accepts anything with a datat() && size() function.