The implementation of these methods seems straightforward to me and they would make usage of std::string
and std::string_view
more interchangeable. After all, std::string_view
has constructors which leave the object in the same state as these methods would. One could workaround the missing methods like this:
std::string s {"abcd"};
std::string_view v {s.c_str()};
std::cout << "ctor: " << v << std::endl; // "abcd"
v = {s.c_str() + 1, 2};
std::cout << "assign: " << v << std::endl; // "bc"
v = {nullptr}; // or even v = {};
std::cout << "clear: " << v << std::endl; // ""
So, what are the reasons for not including these two obvious methods in the standard?
UPDATE:
One general question in your comments seems to be "What's the point?", so I'll give you some context. I'm parsing a large string with the result being a structure of substrings. That result structure is a natural candidate for string views, so I don't have to copy all those strings, which are even overlapping. Part of the result are maps to string views, so I might need to construct them empty when I get the key and fill them later when I get the value. While parsing I need to keep track of intermediate strings, which involves updating and resetting them. Now they could be replaced by string views as well, and that's how I happened on those missing functions. Of course I could continue using strings or replace them by plain old ptr-ptr or ptr-size pairs, but that's exactly what std::string_view
is for, right?
std::string
methods (which is probably the reason you're looking for). Just like changing a pointer to a nullptr is different from deleting the pointed-to object. – Grillparzerstd::vector<int>::clear()
would be semantically different fromstd::vector<int*>::clear()
, no? – Carloscarlotaint
andint*
are trivial to deallocate, there is no semantic difference. For the record, I agree with you that there are "interpretations" ofassign
andclear
where there is no pronounced semantical difference. But you have to agree that it is potentially confusing whetherclear
also clears the viewed string itself - after allstd::string
does that. – Grillparzer