为什么我不能从范围迭代器构造一个 string_view?

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

C++20 为 basic_string_view 添加了一个 constructor,它需要两个迭代器。但是,当我尝试使用来自公共范围的迭代器构建 string_view 时,出现错误。

#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

请注意 iterator constructorstd::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.

您的范围迭代器至少不满足第一个要求