Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Since I'm unfamiliar with the underlying concept, could you give an example of how you could see that being used?


`std::string_view` is essentially something like this:

   struct string_view {
      char *data;
      size_t length;
   }
It is a pointer to a subset of an existing string. It has all of the existing features of `std::string` but passing it around is zero copy. As long as the original memory allocation exists your `string_view`s are still valid. Also, it has an implicit constructor from `const std::string` and `const char #` allowing you you define a single function like `Thing ParseThing(std::string_view line)` and accept `char #` and `std::string` as an input.

More examples are here: https://abseil.io/tips/1

# -> *. HN formats bad


\* (backslash asterisk) will escape the asterisk.

>Also, it has an implicit constructor from `const std::string` and `const char *` allowing you you define a single function like `Thing ParseThing(std::string_view line)` and accept `char *` and `std::string` as an input.


every function where you are reading from a string without writing to it or saving it, you can use std::string_view. Same for vector / array: anything non-owning, read-only should use std::span instead.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: