GreenScape
GreenScape

Reputation: 7745

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

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.

Upvotes: 18

Views: 3850

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474416

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.

Upvotes: 18

Related Questions