简体   繁体   English

仅移动输入和输出迭代器

[英]move-only input and output iterators

Concerning InputIterator / OutputIterator -s is it consistent to modify their semantics to be move-only?关于InputIterator / OutputIterator -s 将它们的语义修改为仅移动是否一致? Surely I mean only newly-created custom iterators, not STL ones.当然,我指的只是新创建的自定义迭代器,而不是STL迭代器。

Notes to semantic requirements to ++i and ++r expressions for input and output iterators correspondingly says:对输入和输出迭代器的++i++r表达式的语义要求的注释相应说:

Postcondition: Any copies of the previous value of i are no longer required to be either dereferenceable or to be in the domain of == .后置条件: i的先前值的任何副本不再需要可取消引用或位于==的域中。

After this operation r is not required to be incrementable and any copies of the previous value of r are no longer required to be dereferenceable or incrementable.在此操作之后, r不需要是可增加的,并且r的先前值的任何副本不再需要是可取消引用或可增加的。

I think it is safer to prohibit possibility to have a copies of input/output iterators in most cases, but never heard such advice.我认为在大多数情况下禁止拥有输入/输出迭代器副本的可能性更安全,但从未听过这样的建议。 Is it bad idea?这是个坏主意吗?

In C++20, the new iterators concepts input_iterator and output_iterator do not require to be copyable.在 C++20 中,新的迭代器概念input_iteratoroutput_iterator不需要可复制。 The algorithms in the ranges namespace, as well as the view adapters support move only iterators too. ranges命名空间中的算法以及视图适配器也仅支持移动迭代器。

The new view ranges::istream_view returns move-only iterator for increased safety.新视图ranges::istream_view返回仅移动迭代器以提高安全性。 Finally the new C++20 view concept does itself not require copyability which allows its own state to be move only.最后,新的 C++20 view概念本身不需要可复制性,它只允许移动自己的状态。

It is still possible to create copyable input_iterator and output_iterator that are not copyable but I would advise not to.仍然可以创建不可copyable copyable input_iteratoroutput_iterator ,但我建议不output_iterator

forward_iterator s are, of course, still required to be copyable .当然, forward_iterator仍然需要是可copyable Sentinels also require to be copyable哨兵也需要可copyable

You can read ore about the design in the following papers您可以在以下论文中阅读有关该设计的内容

Input/OutputIterators must first satisfy the Iterator requirement. Input/OutputIterator 必须首先满足 Iterator 要求。 And that requirement say, from C++14, [iterator.iterators], p2:这个要求说,从 C++14,[iterator.iterators],p2:

A type X satisfies the Iterator requirements if:如果满足以下条件,则类型X满足Iterator要求:

  • X satisfies the CopyConstructible , CopyAssignable , and Destructible requirements (17.6.3.1) X满足CopyConstructibleCopyAssignableDestructible要求 (17.6.3.1)

So no, they cannot be move-only.所以不,他们不能只移动。

That is probably not a good idea.这可能不是一个好主意。 Iterator objects are usually designed to be lightweight and are passed around by value in order to be efficient.迭代器对象通常被设计为轻量级的,并通过值传递以提高效率。 And many of the C++ standard library containers use iterators passed by value.并且许多 C++ 标准库容器使用按值传递的迭代器。 Disabling the copy constructors does not seem like a good idea at all to me.禁用复制构造函数对我来说似乎根本不是一个好主意。

For example see http://www.cplusplus.com/reference/vector/vector/vector/ the vector constructor accepts the input iterator by value.例如,参见http://www.cplusplus.com/reference/vector/vector/vector/向量构造函数按值接受输入迭代器。

And see http://www.cplusplus.com/reference/algorithm/copy/ for an example of a C++ standard library container that accepts an output iterator by value并参阅http://www.cplusplus.com/reference/algorithm/copy/以获取按值接受输出迭代器的 C++ 标准库容器示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM