string_view(end(), end()) should create a legal string_view, but dereferencing end as in the given implementation is UB.
I agree about your assessment of the iterators. And I also thought it was important to insist that the iterators be rand access for a couple of reasons:
Evan,Yes that does address the issue I was raising. Of course, if strings and vectors used pointer iterators then we wouldn't need the if test or the overload at all.
Evan,Yes that does address the issue I was raising. Of course, if strings and vectors used pointer iterators then we wouldn't need the if test or the overload at all.I think this constructor has a lot of room to be accidentally abused, however.One way to work around this is with a "make_*" style utility that uses domain knowledge to convert iterators. For example,string_view make_string_view(array<char>::iterator b, array<char>::iterator e);string_view make_string_view(vector<char>::iterator b, vector<char>::iterator e);string_view make_string_view(string::iterator b, string::iterator e);
Just note that array<char>::iterator may be char *, and so might vector<char>::iterator, and string::iterator.So is that 3 function declarations or 1?
How often do you use iterators with strings?
Any time you have an iterator, you can have a char*, and that it also assuming that the Ran is contiguous, which it isn't garunteed to be.
--
Any time you have an iterator, you can have a char*, and that it also assuming that the Ran is contiguous, which it isn't garunteed to be.