簡體   English   中英

在 range v3 庫中,為什么ranges::copy 不適用於ranges::views::chunk 的output?

[英]In range v3 library, why ranges::copy do not work with output of ranges::views::chunk?

這按預期工作:

#include <range/v3/all.hpp>
#include <iostream>

int main() {
  auto chunklist = ranges::views::ints(1, 13) | ranges::views::chunk(4);
  for (auto rng : chunklist) {
    for (auto elt : rng)
      std::cout << elt << " ";    
    std::cout << std::endl;
  }
}

屏幕上:

1 2 3 4
5 6 7 8
9 10 11 12

但是如果我嘗試替換ranges::copy而不是 for 循環,我會收到編譯錯誤

  auto chunklist = ranges::views::ints(1, 13) | ranges::views::chunk(4);
  for (auto rng : chunklist) {
    ranges::copy(rng, std::ostream_iterator<int>{std::cout, " "}); // FAIL
    std::cout << std::endl;
  }

編譯錯誤相當模糊。 我不會在這里給出全文,但看起來幾乎所有概念都失敗了:

note:   constraints not satisfied
...
note: within '...' CPP_concept_bool weakly_incrementable =
...
note: within '...' CPP_concept_bool semiregular =
...
note: within '...' CPP_concept_bool default_constructible =
...
note: within '...' CPP_concept_bool constructible_from =
...
confused by earlier errors, bailing out

我正在使用帶有選項的 gcc-9.3:

g++ -fconcepts --std=c++2a chunksimplified.cc

以及來自 github 的主干范圍 v3 庫的頂部

我應該如何修改代碼以使用顯式算法而不是原始循環?

問題不在於ranges::copy ,只是ranges::views不能很好地與std::ostream_iterator配合使用。

您可以只使用ranges::ostream_iterator代替:

ranges::copy(rng, ranges::ostream_iterator<int>{std::cout, " "});  

這是一個工作演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM