No implicit conversion from std::string to std::string_view in C++17 (was in std::experimental::basic_string_view)

余生颓废 提交于 2020-08-24 05:11:07

问题


My question is regarding C++17: http://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view

What's the caveat of implicit conversion from std::basic_string to std::basic_string_view that it wasn't included in the interface of the latter?

I believe it would greatly improve this class. Especially the family of comparison operators that, also do not accept std::string as neither lhs nor rhs.

There is such conversion in library fundamentals TS specification: http://en.cppreference.com/w/cpp/experimental/basic_string_view/basic_string_view

This question is about why it was removed. Or rather not adopted.


回答1:


basic_string_view is considered the lower level class. It's the providers of string containers who have the responsibility of providing implicit conversions to string_view. If you have your own string type, then you would give it a possibly explicit operator string_view() overload to perform implicit conversion.

As such, it was decided (PDF) that basic_string would provide the conversion to basic_string_view. The original Library Fundamentals version put the implicit conversion on basic_string_view, because a TS is usually an extension. It can't affect an existing type without effectively forking that type.



来源:https://stackoverflow.com/questions/39207972/no-implicit-conversion-from-stdstring-to-stdstring-view-in-c17-was-in-std

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