【问题标题】:why I can't use a take() after a istream_view in c++20为什么我不能在 c++20 中的 istream_view 之后使用 take()
【发布时间】:2021-09-18 10:06:25
【问题描述】:

代码显示我的问题,take(3)istream_view 之后无法使用。

错误信息是:

/home/linuxbrew/.linuxbrew/Cellar/gcc/11.1.0_1/include/c++/11.1.0/ranges:1775:48: 错误:传递'std::ranges::take_view<:ranges: :transform_view char std::char_traits>, int ()(int)> >::_CI' {aka 'const std::counted_iterator<:ranges: :transform_view char std::char_traits>, int ()(int)>::_Iterator >'} as 'this' 参数丢弃限定符 [-fpermissive] 1775 | { 返回 __y.count() == 0 || __y.base() == __x._M_end; }

#include <ranges>

using namespace std::views;
using namespace std::ranges;
int to_sq(int a){return a*a;}
int main()
{

    auto m_range = istream_view<int>(std::cin);
    // error
    for (auto i : m_range | transform(to_sq)|take(3))
    {
        std::cout << i << std::endl;
    }
}

【问题讨论】:

  • 看起来像是 fixed in trunk 的 gcc 错误。另外,如上所述,请包含to_sq 的定义。

标签: c++ c++20 std-ranges


【解决方案1】:

这是LWG 3391

这里的transform 无关紧要,只是尝试迭代istream_view&lt;int&gt;(std::cin) | views::take(3) 已经是一个问题。从问题:

代码无效,因为ranges::take_view::sentinel::operator==() 必须调用counted_iterator::base() 来比较底层迭代器和它的标记,因此这个operator==() 要求底层迭代器是copy_constructible。

gcc 11.1 尚未包含此修复(特别是 this commit,它在 11.1 发布几周后发布),但 gcc trunk 现在包含。

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2020-04-01
    • 2015-09-03
    • 1970-01-01
    相关资源
    最近更新 更多