【发布时间】:2018-04-24 11:10:11
【问题描述】:
截至 2018 年 3 月 17 日提交 d5e9afc 的 accumulate.hpp
当传递一个范围时,init 会像这样得到一次std::move。
T operator()(Rng && rng, T init, Op op = Op{}, P proj = P{}) const
{
return (*this)(begin(rng), end(rng), std::move(init), std::move(op),
std::move(proj));
}
上面的代码会调用这个:
T operator()(I begin, S end, T init, Op op = Op{}, P proj = P{}) const
{
for(; begin != end; ++begin)
init = invoke(op, init, invoke(proj, *begin)); // why do we need this another copy of init?
return init;
}
我想知道为什么我们在调用调用之前需要另一个 init 副本?
这个初始化必须以任何方式被覆盖,对吧?那么为什么一开始就不能把它撕掉呢?
init = invoke(op, std::move(init), invoke(proj, *begin));
【问题讨论】:
-
invoke是否按值接受? -
@StoryTeller 为什么要禁止将东西作为参数移动?
-
我的猜测是
init旨在成为原始类型,因此按值传递可能会更有效。 -
@Galik 移动非原始税额外周期?或者图书馆试图鼓励使用
init作为一个前言?还是两者兼有?
标签: c++ accumulate range-v3