D
D
Daniel Demidko2018-02-16 09:40:41
C++ / C#
Daniel Demidko, 2018-02-16 09:40:41

Difference between string_view and const string_view&?

What is the difference between string_viewor const string_view&in function arguments?
Does it make sense to use const string_view&instead of string_view?
As I understand it, string_viewthis is a wrapper over the pointer char*.
It turns out that the argument string_viewwill copy the pointer to char*, but const string_view&refer to the pointer?
For years I have used in arguments for strings. I started to join new features and I don’t understand where to stick it . It seems that he wrote, but the hand itself reaches out to remakeconst string&string_viewconst string_view&

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Moseychuk, 2018-02-16
@DanielDemidko

What is the difference between string_view or const string_view& in function arguments?

In the first case, you have an object that you can change. For example set it to a new line. In this case, the original object will remain unchanged, because. here the parameter is passed by value, which means that inside the function you will have a copy of string_view.
In the second case, you have a reference to a constant. You cannot change the object, but you will save a few bytes on the stack.
If the algorithm does not require changing the function parameter, then prefer the second option. A reference to a constant can be obtained from any object.
string_view is by no means a replacement for string. No need to shove it everywhere.
string_view is not a wrapper over char*. string_view may contain the character "\0".
string_view is a read-only pascal string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question