Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] [Longterm] Replace const-ref std::string arg with std::string_view #10110

Open
elad335 opened this issue Apr 13, 2021 · 2 comments
Labels
Longterm Refactoring Refactors or simplifies existing code

Comments

@elad335
Copy link
Contributor

elad335 commented Apr 13, 2021

The difference between the two is that reference references an object of type std::string which is defined to contain a unique buffer for its characters, while std::string_view describes a section of existing memory containing the characters. The previous arguments decleration style makes passing substring and static C-string to a function include an additional and (99% of the time) unneccessary step of allocating new buffer for and copying the characters to it, std::string_view argument does not have this flaw as it is capable of describing these sections of memory without copying them.

@elad335
Copy link
Contributor Author

elad335 commented Apr 13, 2021

This is true for even static constexpr std::string in C++20 when an address is required such as when calling c_str() (compiler cannot optimize it, buffer must be unique in this case). So string_view is recommended for static strings as well.

@Nekotekina
Copy link
Member

For constexpr strings plain old char array can do its job. It's usually not a problem to convert it to string_view on the fly. It feels however we may be missing some kind of nts_view to preserve a null-terminator.

@Megamouse Megamouse added Longterm Refactoring Refactors or simplifies existing code labels Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Longterm Refactoring Refactors or simplifies existing code
Projects
None yet
Development

No branches or pull requests

3 participants