It is possible to create a std::string_view from a std::string easily. But if I want to create a string view of a range of std::string using the iterators of the std::string does not work.
Here is the code that I tried: https://gcc.godbolt.org/z/xrodd8PMq
#include <iostream>
#include <string>
#include <string_view>
#include <iterator>
int main()
{
std::string str{"My String"};
std::string_view strView{str}; // works
//std::string_view strSubView{str.begin(), str.begin() + 2}; // error
}
Of course maybe we can make the substring out from str and use to make a string view strSubView, but there is an extra string creation.
I found that the std::basic_string_view s fifth constructor takes the range of iterators.
template<class It, class End>
constexpr basic_string_view(It first, End last);
But is it only the iterators of the std::string or just std::basic_string_view 's itself? If not for std::string's iterates why shouldn't be we have one, after all the string view:
describes an object that can refer to a constant contiguous sequence of char-like objects !
Taking the range of contiguous sequence of char, should not we count?
std::string_viewcan create substrings too.strSubView = strView.substr( 0, 2 );. That would be comparable to what you are attempting.