logoalt Hacker News

William_BB • yesterday at 11:17 PM • 1 reply • view on HN

To be fair, string_view has one very attractive feature over const ref string. String's char constructor is not explicit. With string view, you are guaranteed to not allocate by accident.


Replies

spacechild1 • today at 4:19 AM

Exactly! When you have a `const std::string&` argument, passing a C-string implicitly allocates a new temporary `std::string`.

`std::string_view` accepts both `const std::string&` and `const char*` without hidden allocations.