18

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.

5
  • C++17 conversion goes in the opposite direction, from string_vew to string. Commented Aug 29, 2016 at 13:55
  • @Cubbi For one, that conversion is explicit. For the second, conversion to std::string would be a rather expensive way to compare std::string_view to std::string. Commented Aug 29, 2016 at 13:58
  • 10
    It was decided that this should be basic_string's responsibility. en.cppreference.com/w/cpp/string/basic_string/… Commented Aug 29, 2016 at 14:02
  • 1
    @T.C. That pretty much makes a full answer to my question. Or rather my complete oversight. Can you make it so? I will accept. Commented Aug 29, 2016 at 14:04
  • 1
    @T.C.: What a prime opportunity to phone in your answer... Commented Aug 29, 2016 at 14:28

1 Answer 1

18

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.