c++ - 为什么 range::accumulate 在调用时不将 init 作为 std::move(init) 传递?
问题描述
截至 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));
解决方案
这段代码试图避免假设它看起来是 C++17。std::move
-inginit
可能会修改它(这就是移动语义在一天结束时的用途)。这给我们留下了这样的东西:
init = /* An expression that maybe modifies init */;
这将导致C++17 之前的未定义行为。range-v3 也将自己宣传为 C++11 和 C++14 的库。
推荐阅读
- sql-server - SQL Server 找不到特殊字符
- python - 如何合并具有不同行和列大小的熊猫数据框?
- kubernetes - 允许 Pod 通过网络策略与其他 Pod 通信
- flutter - 未来方法中的导航颤动
- python - 发生异常时继续循环
- angularjs - 访问 XMLHttpRequest 的问题已被 CORS 策略(Python/Angular)阻止
- android - 如何使用 Kotlin 协程获取 API 错误正文
- laravel - 电子商务的最佳 AWS 服务器
- discord - 如何仅获取频道的ID
- reactjs - 避免函数在 ReactJs 中一次又一次地运行