在范围 v3 库中,为什么 ranges::copy 不适用于 ranges::views::chunk 的输出?
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
的 trunk range-v3 库的顶部
我应该如何修改代码才能使用显式算法而不是原始循环?
问题不在于 ranges::copy
,只是 ranges::views
不能很好地与 std::ostream_iterator
搭配。
您可以只使用 ranges::ostream_iterator
来代替:
ranges::copy(rng, ranges::ostream_iterator<int>{std::cout, " "});
这是一个有效的 demo。
这按预期工作:
#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
的 trunk range-v3 库的顶部我应该如何修改代码才能使用显式算法而不是原始循环?
问题不在于 ranges::copy
,只是 ranges::views
不能很好地与 std::ostream_iterator
搭配。
您可以只使用 ranges::ostream_iterator
来代替:
ranges::copy(rng, ranges::ostream_iterator<int>{std::cout, " "});
这是一个有效的 demo。