Search code examples
c++c++20string-viewstd-ranges

Why can't I construct a string_view from range iterators?


C++20 adds a constructor for basic_string_view which takes two iterators. However, when I try to construct a string_view with the iterators from a common range I get an error.

#include <iostream>
#include <ranges>

using namespace std::views;

int main()
{
    auto words = "the quick brown fox" | split(' ') | transform([]<class RNG>(RNG&& rng) {
        auto v = std::views::common(std::forward<RNG>(rng));
        return std::string_view(v.begin(), v.end());
    });
    for (auto&& word : words) {
        std::cout << word << std::endl;
    }
}

<source>:11:6:   required from here
<source>:10:21: error: no matching function for call to 'std::basic_string_view<char>::basic_string_view(std::common_iterator<std::ranges::split_view<std::ranges::ref_view<const char [20]>, std::ranges::single_view<char> >::_InnerIter<true>, std::default_sentinel_t>, std::common_iterator<std::ranges::split_view<std::ranges::ref_view<const char [20]>, std::ranges::single_view<char> >::_InnerIter<true>, std::default_sentinel_t>)'
10 |         return std::string_view(v.begin(), v.end());
    |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

https://godbolt.org/z/E4779G


Solution

  • Notice the requirements imposed on the iterator constructor for std::string_view:

    This overload only participates in overload resolution if

    • It satisfies contiguous_iterator,
    • End satisfies sized_sentinel_for for It,
    • std::iter_value_t and CharT are the same type, and
    • End is not convertible to std::size_t.

    Your range iterators do not satisfy at least the first requirement