The Ephemeral View: Performance Over Safety in std::string_view Ownership

1 week ago 8
ARTICLE AD BOX

Value category isn't lifetime.

void use1(const std::string&); void use2(std::string_view); auto get() -> std::string; use1(get()); // A. should this be allowed? use2(get()); // B. or this?

Both lines are completely safe, and A is allowed. When adding std::string_view to the language, you have to decide should B be allowed. It is safe, and.it makes it easier to adopt changes like use1 -> use2 which we want to encourage.

On the other hand, just like references, it can be used unsafely. Should we make it harder to use in the way that we want it used, in order to make it harder also to use it in ways that don't make sense?

Or... should we say treat view types like references, so don't store them and pay attention to lifetime?

I wouldn't say this is prioritizing performance over type safety. The type system of C++ is not rich enough to represent lifetime. And yes, that means the developer has to pay attention to lifetime.

wohlstad's user avatar

wohlstad

37.3k19 gold badges80 silver badges117 bronze badges

Jeff Garrett's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article