简体   繁体   English

对于迭代器,Range TS和C ++ 20概念是否需要能够使用`operator->`?

[英]Do the Range TS and C++20 concepts for iterators require the ability to use `operator->`?

I've searched through various Range TS proposals, including P0896, the one incorporating the ranges into C++20. 我搜索了各种Range TS提议,包括P0896,将范围纳入C ++ 20。 It seems from my reading that the only requirement the Iterator concept makes in terms of dereferenceability is that *t be valid syntax that yields an object of some type. 从我的阅读中可以看出, Iterator概念在解除引用性方面的唯一要求是*t是产生某种类型对象的有效语法。

Since InputIterator is defined in terms of being an Iterator and being Readable , neither of which requires operator-> support, it appears that the Range TS and C++20 do not require that iterators provide -> support. 由于InputIterator是作为Iterator定义并且是Readable ,它们都不需要operator->支持,因此Range TS和C ++ 20似乎不要求迭代器提供->支持。

Is this the case? 是这样的吗?

Yes, we've dropped the operator-> requirement from InputIterator , and consequently the iterator concepts that refine it. 是的,我们从InputIterator删除了operator-> InputIterator ,从而删除了优化它的迭代器概念。 (The requirement remains part of the "old" input iterator requirements, which are unchanged.) There are a number of reasons: (该要求仍然是“旧”输入迭代器要求的一部分,它们没有变化。)有很多原因:

  1. There's no way to implement -> for many iterator types such that the semantics of i->m are equivalent to (*i).m as the "old" requirements expect . 没有办法实现->对于许多迭代器类型,使得i->m的语义等同于(*i).m 作为“旧”要求所期望的 move_iterator is a good example: (*i).m is an rvalue, whereas i->m is an lvalue. move_iterator是一个很好的例子: (*i).m是一个右值,而i->m是一个左值。 (Yes, it's yet another Standard iterator that doesn't satisfy the iterator requirements.) (是的,它是另一个不满足迭代器要求的标准迭代器。)
  2. There's no way to usefully constrain -> with concepts. 没有办法有效地约束->概念。 Sure, we could require that there is an operator-> , but we couldn't constrain it to have reasonable syntax. 当然,我们可以要求一个operator-> ,但我们不能限制它有合理的语法。
  3. Most importantly, -> is useless to the standard algorithms: they have no idea if the elements denoted by an iterator have members, let alone how to name such members. 最重要的是, ->对标准算法没用:他们不知道迭代器表示的元素是否有成员,更不用说如何命名这些成员了。

This doesn't mean that standard iterators won't provide operator-> (Although see LWG 2790 ), only that iterators aren't required to implement such an operator to be usable with the standard library. 这并不意味着标准迭代器不会提供operator-> (尽管参见LWG 2790 ),只是不需要迭代器来实现这样的运算符以便与标准库一起使用。

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

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