forward_iterator模板类有意义吗? [英] Does a forward_iterator template class make sense?

查看:464
本文介绍了forward_iterator模板类有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准库包含方便的模板类 std :: move_iterator 。考虑到 std :: move std :: forward 之间的密切关系,为什么没有等价的 std :: forward_iterator ?示例用法:

The C++ standard library contains the convenient template class std::move_iterator. Given the close relationship between std::move and std::forward, why is there no equivalent std::forward_iterator? Example usage:

template <typename C>
auto foo(C&& values)
{
    remove_reference_t<C> result {};
    result.reserve(values.size());
    std::transform(std::make_forward_iterator<C>(std::begin(values)),
                   std::make_forward_iterator<C>(std::end(values))), 
                   std::back_inserter(result), Bar());
    return result;
}

这个想法现在可以使用 foo like:

The idea being I can now use foo like:

std::vector<ComplexType> values {/* lots of values */};

auto copied_fooed = foo(values); 
// or
auto moved_fooed  = foo(std::move(values));

无需写两个 foo

推荐答案

比这更复杂。

一个拥有的容器,容器的l / r的意味着移动迭代器可能是有用的。

With an owning container, the l/r valueness of the container imply move iterators may be of use.

但是对于视图,这不工作:从右值视图移动是

But for views, this does not work: moving from an rvalue view is destructive of unowned state.

实际上,这意味着基于l / r的价值决定是否移动是上下文和值相互决定的问题,这使得它很棘手。

Practically, this means the decision to move or not based on l/r valueness is matter for both the context and value to mutually decide, which makes it tricky.

Range-v3和概念可能使这个更容易推理。

Range-v3 and concepts may make this easier to reason about.

这篇关于forward_iterator模板类有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆