logoalt Hacker News

spacechild1 • yesterday at 11:10 PM • 1 reply • view on HN

`string_view` (and `std::span`) is great! Finally I don't have to use concrete types like `const std::string&` or `const std::vector<float>&` as function parameters when all I really want is just something that behaves like a string or array/vector type.

In fact, `std::string_view` can lead to very elegant and efficient (zero-allocation) string parsing code.

IME, taking a string_view as an argument is pretty unproblematic. Two areas where I have seen problems:

1. returning a string_view from a function. Think twice before doing that.

2. calling data() and passing the result to a function that expects a null-terminated C strings. (std::string_view is not guaranteed to be null-terminated.)

---

Apart from that, there are so many great features and libraries in C++17 and C++20 that I use on a regular basis and that I would really miss:

- structured bindings

- if-statement initializers

- fold expressions

- inline variables (very useful for handling global state in header-only stuff)

- if constexpr (replaces many uses of SFINAE)

- concepts (replaces all of the remaining uses of SFINAE)

- coroutines (really nice to use with ASIO)

- std::optional

- std::variant

- std::filesystem

- std::format (although I personally stick to fmt::format)

- from_chars() and to_chars() (fast, local-independent string-number conversion routines)

- std::bit

- std::apply, std::invoke, std::bind_front

- etc.


Replies

Joker_vD • today at 2:42 AM

> 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.

➕ show 1 reply