logoalt Hacker News

Joker_vD • today at 2:42 AM • 1 reply • view on HN

> all I really want is just something that behaves like a string or array/vector type.

What does behave like a std::string/std::vector but is not std::string/std::vector, respectively? In my experience, if you want a flat array behind a T* (or std::dequeue<T> at the worst) — so that the autovectorization can kick in — then that's precisely what you want, and no generalization to e.g. a linked list, or a heap, or a red-black tree is really possible.


Replies

spacechild1 • today at 4:16 AM

> What does behave like a std::string/std::vector but is not std::string/std::vector, respectively?

`std::span` works with all vector/array-like types: `std::vector`, `std::array`, C-arrays and third-party vector/array types.

Similarly, `std::string_view` works with `std::string`, C-strings and third-party string types.

> so that the autovectorization can kick in

It's not even about vectorization. Often you just have a function that takes a string-like or array-like thing, but you don't care about the exact container. Yes, you could make the argument a template (and constrain it with concepts), but then you have to put everything in headers.