Is there sense in using const std::string& arguments in C++17?

青春壹個敷衍的年華 提交于 2020-01-01 03:53:11

问题


By getting string_view in C++17 we got cheap method of passing both std::string and char* to functions that do not take ownership of the string and avoid making temporary copies. By using std::string passed by value and std::move we get explicit and fast passing of string ownership for both r-value and l-value references.

My question is: is there any benefit in using const std::string& as any function parameter in new C++ standard?


回答1:


Yes.

The problem with std::string_view is that it doesn't remember if it points to a null-terminated string or not.

If you're writing a wrapper for a C api that uses null-terminated strings, you would have to constantly copy your std::string_views into std::strings to make sure you have null-terminators.



来源:https://stackoverflow.com/questions/57951871/is-there-sense-in-using-const-stdstring-arguments-in-c17

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!