r/cpp • • Apr 24 '23

The View of The String

https://cppsenioreas.wordpress.com/2023/04/24/the-view-of-the-string/

std::string_view can optimize both performance and code readability in code sections which handle strings. However it can also lead to UB and to memory issues if used incorrectly.

0 Upvotes

13 comments sorted by

4

u/RoyBellingan Apr 24 '23

However it can also lead to UB and to memory issues if used incorrectly.

Like everything under the sun

1

u/CoralKashri Apr 24 '23

The question is how easy is it to use it incorrectly 😅

2

u/RoyBellingan Apr 24 '23 edited Apr 24 '23

In my case, the CSV decoder I have implemented, takes ownership of the raw csv, and return only view to the splitted part.

If you now terminate life of the decoder and pass around the view, well... Yes can be misused but libasan and other analyzer will immediately catch it.

But I do agree with you, if the speed benefit is not noticeable, is better to play safe.

Like in the initial config processing, here I can not care less to use such decoder style.

20

u/IyeOnline Apr 24 '23

Rule #1: Never return a std::string_view

I disagree with the conclusion in the last paragraph here.

Returning a std::string_view is no different than returning a reference from other functions. If you saw your logic here through to the end, then operator[] or any getterse should not return references either.

1

u/arka2947 Apr 24 '23 edited Apr 25 '23

Modern Cpp encourages the use of auto, consider:

auto var = thing.get()

If ret val is std::string this is safe, if string_view, possible foot gun.

Sadly i agree with the dont return string_view opinion.

4

u/ioctl79 Apr 25 '23

IMO, this is an excellent illustration of why using auto everywhere is problematic: C++ is all about types; hiding types causes problems.

1

u/tisti Apr 25 '23
auto var = std::string{thing.get()}

This nicely follows the DRY principle as well.

2

u/Fulgen301 Apr 25 '23

What's wrong with std::string var{thing.get()}?

1

u/tisti Apr 26 '23

Same thing, so it comes down to personal preference.

0

u/[deleted] Apr 24 '23

[deleted]

11

u/IyeOnline Apr 24 '23

I agree that returning a view (or a reference for that matter) can be dangerous if people missunderstand it.

I was specifically talking about that last sentence:

so the safe way is to forbid returning of std::string_view in any case.

I suppose it is the safe way, but its not reasonable/consistent with other situations where the same logic would apply.

The same logic of "it can become invalid in the future" would also apply to vector::operator[](...) const, but you obviously wouldnt apply the logic there.

We generally assume that the referenced/viewed object we return will remain valid for as long as people hold a reference to it, or rather that its in the users responsibility to ensure that.

0

u/[deleted] Apr 24 '23

[deleted]

7

u/IyeOnline Apr 24 '23

Maybe I am just expecting too much. :)

I would generally always expect people to be aware of their object lifetimes.

But as you say in the article, it is the safe rule to just not return string_views. Doing that certainly avoids potential bugs. I just dont think that that safety is worthwhile compared to the costs incured otherwise (e.g. when the alternative would be to return a full std::string object by value).

0

u/[deleted] Apr 24 '23

[deleted]

8

u/IyeOnline Apr 24 '23

std::vector::at

Another point we'd disagree on.

I would never use that. Either your program is correct and you dont need bounds checking, or its not and you should be debugging it instead. For debugging, all standard library implementations provide the possibility to augment operator[].

But I do get where you are coming from. Better safe and slightly less efficient than sorry and crashing.

1

u/CoralKashri Apr 24 '23

Yes, especially if I should debug others' mistakes eventually 😅