为什么按值捕获 std::istringstream 右值的 Op 不符合 Accumulateable 概念?
Why does Op that captures rvalue of std::istringstream by value fails the Accumulateable concept?
这不会编译:
auto acc_func = [iss{std::istringstream{}}](int acc, std::string &str) mutable {
iss.str(str);
int sz;
iss >> sz;
iss.clear();
return acc + sz;
};
ranges::getlines_range lazy_lines = ranges::getlines(std::cin);
auto rng = lazy_lines | ranges::view::all;
auto begin = ranges::begin(rng);
auto end = ranges::end(rng);
auto acc = ranges::accumulate(begin, end, 0, acc_func);
std::cout << acc;
错误为:
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/numeric/accumulate.hpp:39:15: note: candidate template ignored: requirement 'Accumulateable<ranges::v3::_basic_iterator_::basic_iterator<ranges::v3::getlines_range::cursor>, int, (lambda at <source>:9:21), ranges::v3::ident>()' was not satisfied [with I = ranges::v3::_basic_iterator_::basic_iterator<ranges::v3::getlines_range::cursor>, S = ranges::v3::default_sentinel, T = int, Op = (lambda at <source>:9:21), P = ranges::v3::ident, _concept_requires_38 = 42]
T operator()(I begin, S end, T init, Op op = Op{}, P proj = P{}) const
^
然而,通过引用捕获是没问题的。
std::istringstream stack_iss;
auto acc_func = [&iss{stack_iss}](int acc, std::string &str) mutable {
iss.str(str);
int sz;
iss >> sz;
iss.clear();
return acc + sz;
};
ranges::getlines_range lazy_lines = ranges::getlines(std::cin);
auto rng = lazy_lines | ranges::view::all;
auto begin = ranges::begin(rng);
auto end = ranges::end(rng);
auto acc = ranges::accumulate(begin, end, 0, acc_func);
std::cout << acc;
为什么按值捕获 std::istringstream
的右值不符合 Accumulateable
概念?
提供给所有算法的仿函数必须可复制。 IOstream 类型不可复制;他们只能移动。因此,任何包含它们的 lambda 都是不可复制的。
此外,您还可以在函子内部创建 istringstream
;在每个周期中清除它并插入一个新字符串并不比 constructing/destructing 它们便宜。
这不会编译:
auto acc_func = [iss{std::istringstream{}}](int acc, std::string &str) mutable {
iss.str(str);
int sz;
iss >> sz;
iss.clear();
return acc + sz;
};
ranges::getlines_range lazy_lines = ranges::getlines(std::cin);
auto rng = lazy_lines | ranges::view::all;
auto begin = ranges::begin(rng);
auto end = ranges::end(rng);
auto acc = ranges::accumulate(begin, end, 0, acc_func);
std::cout << acc;
错误为:
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/numeric/accumulate.hpp:39:15: note: candidate template ignored: requirement 'Accumulateable<ranges::v3::_basic_iterator_::basic_iterator<ranges::v3::getlines_range::cursor>, int, (lambda at <source>:9:21), ranges::v3::ident>()' was not satisfied [with I = ranges::v3::_basic_iterator_::basic_iterator<ranges::v3::getlines_range::cursor>, S = ranges::v3::default_sentinel, T = int, Op = (lambda at <source>:9:21), P = ranges::v3::ident, _concept_requires_38 = 42]
T operator()(I begin, S end, T init, Op op = Op{}, P proj = P{}) const
^
然而,通过引用捕获是没问题的。
std::istringstream stack_iss;
auto acc_func = [&iss{stack_iss}](int acc, std::string &str) mutable {
iss.str(str);
int sz;
iss >> sz;
iss.clear();
return acc + sz;
};
ranges::getlines_range lazy_lines = ranges::getlines(std::cin);
auto rng = lazy_lines | ranges::view::all;
auto begin = ranges::begin(rng);
auto end = ranges::end(rng);
auto acc = ranges::accumulate(begin, end, 0, acc_func);
std::cout << acc;
为什么按值捕获 std::istringstream
的右值不符合 Accumulateable
概念?
提供给所有算法的仿函数必须可复制。 IOstream 类型不可复制;他们只能移动。因此,任何包含它们的 lambda 都是不可复制的。
此外,您还可以在函子内部创建 istringstream
;在每个周期中清除它并插入一个新字符串并不比 constructing/destructing 它们便宜。