-
Notifications
You must be signed in to change notification settings - Fork 523
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
C++: return std::string_view instead of char* for variable length strings #550
Comments
The string view is a nice feature. However requiring C++ 17 might be too much of an ask. We often get requests to provide back ports to C++ 98 and before! |
#if __cplusplus > 201402L
...
#endif Or make something like in |
A simple version check would be sufficient. As @ksergey mentions. But, I would not remove the method to return the |
|
@tkohlman I am aware.
|
Is the That being said, I understand a desire to maintain backwards compatibility in the API. For my purposes, I would be happy with a |
C++17 is a full stop for most users. I think you are making a lot of assumptions that don't hold in the usages I know of. The Grabbing the length doesn't move the position. And the redundant memcpy is easily elided. All the optimizations you mention are also possible without In my experience, what could be optimized usually means the optimizer can be easily defeated on. Simple straight forward logic, though, is usually more easily and predictably optimized. Especially in large projects. But as with anything, measuring in actual usage in an app has no comparison. The use of |
I nice optimization and/or enhancement would be to return
std::string_view
instead ofconst char*
for variable length strings. Applications may wish to use a string without copying its bytes somewhere else.To achieve this now, code must read and save the string's length, then read the string, then construct the string_view.
It cannot be done in a single line, because the length field must be read first. Also note that the length is copied into a uint32_t field twice, once in
message.usernameLength()
and a second time inmessage.username()
. Returningstd::string_view
would eliminate one of the copies.It would be nice to be able to do one of the following:
This might require a C++17 compatibility flag of some kind.
The text was updated successfully, but these errors were encountered: