There is an implicit conversion from std::string
to std::string_view
and it's not considered unsafe, even though this surely may cause a lot of dangling references if the programmer is not careful.
On the other hand, there's no implicit conversion from std::string_view
to std::string
using same argument but in the completely opposite fashion: because the programmer may not be careful.
It's lovely that C++ has a replacement for a raw const char*
pointer, while making it super confusing and stripped to the bone:
- Implicit
const char*
->std::string
: OK - Implicit
std::string_view
->std::string
: NOPE - Assignment
std::string
=const char*
: OK - Assignment
std::string
=std::string_view
: OK - Appending
std::string
+=const char*
: OK - Appending
std::string
+=std::string_view
: OK - Concatenation
const char*
+std::string
: OK - Concatenation
std::string_view
+std::string
: NOPE - Concatenation
std::string
+const char*
: OK - Concatenation
std::string
+std::string_view
: NOPE
Am I missing something or is this a total nonsense?
In the end, how useful is this string view without all the crucial pieces that make it similar to const char*
? What's the point of integrating it into the ecosystem of stdlib while not making the last step to make it complete? After all, if we need an object that represents a piece of a string we could write our own. Actually, a lot of libraries already have done that, years ago. The whole point of making something standard is to make it useful for widest range of use cases, isn't it?
Are they going to fix this in C++23?